How to access one component's state from another component

前端 未结 5 650
遥遥无期
遥遥无期 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 07:52

    I would suggest you use a state manager like Redux (personal favorite), MobX reflux, etc to manage your state.

    How these works is they allow you to contain all shared state in one state storage (called a store), and whatever component needs access to a part of that shared state, it will just get it from the store.

    It looked very hard to get started with but once you get over the small challenges, get 2 or 3 "wtf's" out of the way. It gets easier.

    Take a look here: http://redux.js.org/

    EDIT: Redux is good but the boilerplate code is really a turn off... for those of you looking for a simpler, more magical (this can be good and bad) solution use mobx : https://mobx.js.org/

提交回复
热议问题