我试图将一些数据从 state 中作为道具传递给另一个组件,如下所示:
class Editor extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
Data: null
};
}
handleClick = () => {
const fdata = this.props.fetchData(); //returns some data as array of objects
this.setState({
Data: fdata
});
};
render() {
<Overview sos={this.state.Data} />; //trying to pass Data from state to another component
}
}
出错Uncaught TypeError: Cannot read property '_props' of undefined
。
首先让你的状态对象变量以小写字母开头
this.state = {data: null}
其次,您的 handleClick 具有异步逻辑,因此请使用 async 和 await。像这样 -
handleClick = async () =>{
const fdata = await this.props.fetchData(); //returns some data as array of objects
this.setState({
data: fdata
});
}
编辑:正如@rrd 提到的,你需要在渲染中返回 jsx
render{
return(
....
)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句