Ts-react应用技巧
TS 中使用 typeof 自动获取数据类型
在写 React 项目时,有些时候,你的 state 可能会有默认值,比如:
const initialState = {
username: '',
mobile: '',
isVip: false,
addresses: []
}
type IState = typeof initialState
class Comp extends Component<any, IState> {
constructor(props) {
super(props)
this.state = {
...initialState
}
}
}
这样就不用分开定义 state 的初始值和 state 的类型了。