how can we pass state from child compo to parent component code example
Example 1: how to pass props from child to parent
class ToDoList extends React.Component { constructor(props) { super(props); this.state = { listDataFromChild: null }; }, myCallback = (dataFromChild) => { this.setState({ listDataFromChild: dataFromChild }); }, otherFn = () => { [...within this other function now I still have access to this.state.listDataFromChild...] } render() { return ( <div> <ToDoItem callbackFromParent={this.myCallback}/> [...now here I can pass this.state.listDataFromChild as a prop to any other child component...] </div> ); }});
Example 2: how to pass state from parent to child in react
class SomeParentComponent extends React.Component {
constructor(props) {
super(props);
this.state = {color: 'red'};
}
render() {
return <SomeChildComponent color={this.state.color} />;
}
}