React-Redux: Should all component states be kept in Redux Store

后端 未结 4 1900
无人共我
无人共我 2020-12-04 06:39

Say I have a simple toggle:

When I click the button, the Color component changes between red and blue

I might achieve this result by doing somethin

4条回答
  •  孤城傲影
    2020-12-04 07:19

    For the purpose of highlighting the great link provided by @AR7, and because that link moved a while back:

    Use React for ephemeral state that doesn't matter to the app globally and doesn't mutate in complex ways. For example, a toggle in some UI element, a form input state. Use Redux for state that matters globally or is mutated in complex ways. For example, cached users, or a post draft.

    Sometimes you'll want to move from Redux state to React state (when storing something in Redux gets awkward) or the other way around (when more components need to have access to some state that used to be local).

    The rule of thumb is: do whatever is less awkward.

    Dan Abramov: https://github.com/reactjs/redux/issues/1287#issuecomment-175351978

提交回复
热议问题