How to update parent's state in React?

后端 未结 14 1723
粉色の甜心
粉色の甜心 2020-11-22 08:48

My structure looks as follows:

Component 1  

 - |- Component 2


 - - |- Component 4


 - - -  |- Component 5  

Component 3

Component 3 s

14条回答
  •  生来不讨喜
    2020-11-22 09:41

    I want to thank the most upvoted answer for giving me the idea of my own problem basically the variation of it with arrow function and passing param from child component:

     class Parent extends React.Component {
      constructor(props) {
        super(props)
        // without bind, replaced by arrow func below
      }
    
      handler = (val) => {
        this.setState({
          someVar: val
        })
      }
    
      render() {
        return 
      }
    }
    
    class Child extends React.Component {
      render() {
        return 

    Hope it helps someone.

提交回复
热议问题