When do I choose React state Vs Redux Store

后端 未结 3 928
感动是毒
感动是毒 2020-12-02 22:37

I\'ve been learning Redux and a part I\'m unclear of is, how do I make a determination between using react state vs redux store and then dispatching actions. from my reading

3条回答
  •  广开言路
    2020-12-02 23:05

    From book:

    First of all, we should always keep in mind that only the minimal amount of data needed should be put into the state. For example, if we have to change a label when a button is clicked we should not store the text of the label, but we should only save a Boolean flag that tells us if the button has been clicked or not. Secondly, we should add to the state only the values that we want to update when an event happens, and for which we want to make the component re-render. Another way to figure out whether the state is the right place to store information is to check if the data we are persisting is needed outside the component itself or by its children. If multiple components need to keep track of the same information, we should consider using a state manager like Redux at the application level.

提交回复
热议问题