Answer the question
In order to leave comments, you need to log in
How to change parent state from child React component?
I pass the state of the parent to the child component:
Next, in the child component, there is an element on click on which a method is called that should change the state of the parent to true.
How to do it? <Component openModal={this.state.openModal} />
Answer the question
In order to leave comments, you need to log in
The correct solution was suggested, I will disassemble it a little for understanding.
<Parent>
<Children openModal={this.state.openModal} onChange={this.onChange} />
<Parent />
onChange=()=>{
this.setState({openModal : true})
}
<div onClick={this.props.onChange}>
...какие-то элементы...
</div>
<Component openModal={this.state.openModal} onChange={this.onChange} />
1) create a handler in the parent component that will change the state.
2) pass the handler to the child component.
3) use the handler in the child component and change the State in the parent component.
In addition to the state, also throw a function from the parent there
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question