Reputation: 65
I am trying to pass some data from state, as props to another component as follows:
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
.
Upvotes: 0
Views: 274
Reputation: 3690
First of all make your state object variables starting letter lowercase that is
this.state = {data: null}
Second your handleClick has async logic so make use of async and await. 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
render{
return(
....
)
}
Upvotes: 0
Reputation: 5957
Since it's a class you need to return something in the render:
render() {
return (
<Overview ...
);
}
Is there something loading this class too, passing props into it?
Upvotes: 1