[英]Uncaught TypeError: Cannot read property '_props' of undefined
I am trying to pass some data from state, as props to another component as follows:我正在尝试将 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
}
}
Getting an error Uncaught TypeError: Cannot read property '_props' of undefined
.收到错误
Uncaught TypeError: Cannot read property '_props' of undefined
。
Since it's a class you need to return something in the render:由于它是 class 您需要在渲染中返回一些内容:
render() {
return (
<Overview ...
);
}
Is there something loading this class too, passing props into it?是否也有加载这个 class 的东西,将道具传递给它?
First of all make your state object variables starting letter lowercase that is首先让你的 state object 变量开始字母小写
this.state = {data: null}
Second your handleClick has async logic so make use of async and await.其次,您的 handleClick 具有异步逻辑,因此请使用异步和等待。 Like this -
像这样 -
handleClick = async () =>{
const fdata = await this.props.fetchData(); //returns some data as array of objects
this.setState({
data: fdata
});
}
EDIT: And as @rrd mentioned you need to return the jsx inside render编辑:正如@rrd 提到的,您需要在渲染中返回 jsx
render{
return(
....
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.