我正尝试将状态数据作为道具传递给另一个组件,如下所示:
this.categories= [
"Horror1"
"Horror2"
"Horror3"
]
遇到错误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
}
}
。
答案 0 :(得分:1)
因为它是一个类,所以您需要在渲染器中返回一些内容:
render() {
return (
<Overview ...
);
}
是否也需要加载此类,将道具传递给它?
答案 1 :(得分:0)
首先使状态对象变量的首字母小写
this.state = {data: null}
第二个handleClick具有异步逻辑,因此请使用异步并等待。像这样-
handleClick = async () =>{
const fdata = await this.props.fetchData(); //returns some data as array of objects
this.setState({
data: fdata
});
}
编辑:正如@rrd所述,您需要在render内返回jsx
render{
return(
....
)
}