在 Redux 存储中保持用户界面状态的好处?
Benefit of keeping user interface state in a Redux store?
NGRX official demo application 在商店中保持布局状态。
这个 Akita Article 在秋田 EntityStore<Book>
中也保持状态 searchTerm
等。
只是想知道在商店中保留用户界面状态是否有其他好处,而不是仅在 UI 个组件中?
根据 Redux FAQ entry on how to split state between the store and components:
There is no “right” answer for this. Some users prefer to keep every single piece of data in Redux, to maintain a fully serializable and controlled version of their application at all times. Others prefer to keep non-critical or UI state, such as “is this dropdown currently open”, inside a component's internal state.
Using local component state is fine. As a developer, it is your job to determine what kinds of state make up your application, and where each piece of state should live. Find a balance that works for you, and go with it.
Some common rules of thumb for determining what kind of data should be put into Redux:
- Do other parts of the application care about this data?
- Do you need to be able to create further derived data based on this original data?
- Is the same data being used to drive multiple components?
- Is there value to you in being able to restore this state to a given point in time (ie, time travel debugging)?
- Do you want to cache the data (ie, use what's in state if it's already there instead of re-requesting it)?
需要考虑的一件事是,当您使用组件状态并且组件被卸载并再次重新安装时,它的状态通常会丢失。如果组件将其所有状态存储在 redux 中,则可以保留它的状态。
当我将 react-router 与 redux 结合使用时,这通常是一个因素。我想离开某些页面并 return 以相同的状态导航到它们。
NGRX official demo application 在商店中保持布局状态。
这个 Akita Article 在秋田 EntityStore<Book>
中也保持状态 searchTerm
等。
只是想知道在商店中保留用户界面状态是否有其他好处,而不是仅在 UI 个组件中?
根据 Redux FAQ entry on how to split state between the store and components:
There is no “right” answer for this. Some users prefer to keep every single piece of data in Redux, to maintain a fully serializable and controlled version of their application at all times. Others prefer to keep non-critical or UI state, such as “is this dropdown currently open”, inside a component's internal state.
Using local component state is fine. As a developer, it is your job to determine what kinds of state make up your application, and where each piece of state should live. Find a balance that works for you, and go with it.
Some common rules of thumb for determining what kind of data should be put into Redux:
- Do other parts of the application care about this data?
- Do you need to be able to create further derived data based on this original data?
- Is the same data being used to drive multiple components?
- Is there value to you in being able to restore this state to a given point in time (ie, time travel debugging)?
- Do you want to cache the data (ie, use what's in state if it's already there instead of re-requesting it)?
需要考虑的一件事是,当您使用组件状态并且组件被卸载并再次重新安装时,它的状态通常会丢失。如果组件将其所有状态存储在 redux 中,则可以保留它的状态。
当我将 react-router 与 redux 结合使用时,这通常是一个因素。我想离开某些页面并 return 以相同的状态导航到它们。