How to access one component's state from another component

前端 未结 5 653
遥遥无期
遥遥无期 2020-12-09 07:51

How do I access one component\'s state in another component? Below is my code and I\'m trying to access the state of component a in component b.

5条回答
  •  没有蜡笔的小新
    2020-12-09 08:17

    If two components need access to the same state they should have a common ancestor where the state is kept.

    So component A is the parent of B and C. Component A has the state, and passes it down as props to B and C. If you want to change the state from B you pass down a callback function as a prop.

提交回复
热议问题