【发布时间】:2018-11-28 20:32:46
【问题描述】:
我正在尝试创建多个组件以供将来渲染,将 tham 添加到数组中,如下所示:
widgets.push(<TextWidget fieldData={fieldData} correctionFactor={correctionFactor} />);
但在我的组件中我得到了
TypeError:无法设置未定义的属性“FieldData”
class TextWidget extends Component {
FieldData = null;
CorrectionFactor = null;
state = {
FieldData: null,
CorrectionFactor: null
}
constructor(props) {
this.FieldData = props.fieldData;
this.CorrectionFactor = props.correctionFactor || 1;
}
componentDidMount() {
this.state.FieldData = this.FieldData;
this.state.CorrectionFactor = this.CorrectionFactor;
}
....
如果我在构造函数中像 this.state.FieldData = props.FieldData; 那样做某事,那么 react 会抱怨无法设置未安装组件的状态。
【问题讨论】:
标签: reactjs