I'm just starting out with React, adapting the tic tac toe tutorial for my case. I'm trying to click on the grandchild component to change the state of the grandparent component . Code is as follows:
class App extends React.Component { constructor(props) { super(props); this.state = { fields: [ { id: 1, show: false }, { id: 2, show: false } ] } } handleClick(i) { const fields = this.state.fields.slice(); fields[i].show = true; this.setState({fields: fields}); } render() {return <Preview />} } const Preview = (props) => { return ( <div className="preview"> {props.fields.map((field) => ( <Field data={field} key={field.id} onClick={ props.onClick(field.id) }/> ))} </div> ); }; const Field = props => { return ( <div className="field" onClick={ props.onClick } /> ); }; I get a TypeError: Cannot read property 'state' of undefined from this line:
handleClick(i) { const fields = this.state.fields.slice();
<Preview />