【发布时间】:2019-09-23 03:36:47
【问题描述】:
我将 Flow 与 React 一起使用,并且我有一个使用 State 和 Props 类型的类,如下所示:
type B = {x:number}
type State = {
a: ?B
};
type Props = {}
class MyClass extends React.PureComponent<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
a: null,
};
}
...
myFunction=()=>{
console.log(this.state.a.x)// Flow error here
}
}
流错误是:无法获取this.state.a.x,因为未定义中缺少属性x。我的类型定义有什么问题?为什么我应该在构造函数中使用“道具”类型定义(道具:道具){}?
【问题讨论】: