React Hooks 全解析:从类组件迁移到功能组件
1. 类组件迁移到 React Hooks
1.1 示例背景
我们以从 GitHub 仓库获取问题并列出它们的代码为例,将当前使用类组件和一些生命周期方法的代码转换为使用 React Hooks 的功能组件。
1.2 安装依赖
首先,我们需要安装 axios 来执行数据获取操作:
npm install axios
1.3 类组件代码示例
// Dependencies
import { Component } from 'react'
import axios from 'axios'
// Types
type Issue = {
number: number
title: string
state: string
}
type Props = {}
type State = { issues: Issue[] };
class Issues extends Component<Props, State> {
constructor(props: Props) {
super(props)
this.state = {
issues: []
}
}
componentDidMount() {
axios
.get('https://api.github.com/r
超级会员免费看
订阅专栏 解锁全文
1357

被折叠的 条评论
为什么被折叠?



