我正在开发 React Native 应用程序。我遇到了用于应用程序状态管理的 redux。
假设我的应用程序中有欢迎游览,这应该是一次查看 Activity 。我可以将其保存为正常状态并 checkin 我的组件。
这也可以使用 redux 来实现,那么 redux 与正常状态有何不同。任何用例?
最佳答案
React Native 中的 Redux 与普通 React 应用程序中的 Redux 没有什么不同,因此所有关于 Redux 与本地状态的建议都适用。
引用 Redux 文档:
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 作者 Dan Abramov 的话:
If you’re just learning React, don’t make Redux your first choice. Instead learn to think in React. Come back to Redux if you find a real need for it, or if you want to try something new. But approach it with caution, just like you do with any highly opinionated tool.
还有我最喜欢的:
The rule of thumb is: do whatever is less awkward.
来源:
关于android - React Native 中 redux over normal state 机制的用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46609838/