How to pass state back to parent in React?

后端 未结 7 1420
时光说笑
时光说笑 2020-12-05 04:08

I have a form that has a submit button. That form calls a function onclick that sets the state of something from false to true. I then want to pass this state back to the pa

7条回答
  •  无人及你
    2020-12-05 04:54

    Move handleClick to the parent and pass it to the child component as a prop.

    
    

    Now in the child component:

    This way submitting the form will update the state in parent component directly. This assumes you don't need to access updated state value in child component. If you do, then you can pass the state value back from the parent to the child as a prop. One-way data flow is maintained.

    
    

提交回复
热议问题