什么时候选择 React state 和 Redux Store
When do I choose React state Vs Redux Store
我一直在学习 Redux,但我不清楚的部分是,我如何在使用 React 状态与 Redux 存储然后调度操作之间做出决定。从我目前的阅读来看,我似乎可以使用 React state 代替 Redux store,并且仍然可以完成工作。我理解使用 Redux store 和只有 1 个容器组件而其余部分作为无状态组件的关注点分离,但是我如何确定何时使用 React state 与 redux store 并不是很清楚。有人可以帮忙吗?
谢谢!
你完全正确。 Redux(和一般的 flux 架构)只是帮助构建大型应用程序的形式主义工具。它们根本没有必要。
实际上有一个有趣的 post 叫做 You might not need redux by Dan Abramov,redux 的创建者可能会给你比我更好的答案:https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367#.7093fm1z8
如果状态不需要与其他组件共享,或者组件卸载时不需要保留状态,则可以将其放在组件的状态中。
你可以认为 Redux store 是前端的数据库,如果你有从 API 中获取产品数据之类的东西,那么 Redux store 就是正确的地方;如果你有一个下拉组件,它需要一个 isOpen
属性,那么该下拉列表的父级可以保持 dropdownIsOpen
作为组件状态。
更多信息,这里是丹的回答:https://github.com/reactjs/redux/issues/1287
你也说
only 1 container component and the rest of it as stateless component
这是不正确的。您可以有多个容器组件。一个容器组件还可以包含另一个容器组件。
来自书本:
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.
如果只有特定组件需要状态,那么更喜欢使用反应状态。
例如使用 UI 动作
的状态
如果您想在 component/project 中使用任何状态,请使用 redux 状态。
例如API响应数据等
有关详细信息,请参阅此 link
https://spin.atomicobject.com/2017/06/07/react-state-vs-redux-state/
我一直在学习 Redux,但我不清楚的部分是,我如何在使用 React 状态与 Redux 存储然后调度操作之间做出决定。从我目前的阅读来看,我似乎可以使用 React state 代替 Redux store,并且仍然可以完成工作。我理解使用 Redux store 和只有 1 个容器组件而其余部分作为无状态组件的关注点分离,但是我如何确定何时使用 React state 与 redux store 并不是很清楚。有人可以帮忙吗?
谢谢!
你完全正确。 Redux(和一般的 flux 架构)只是帮助构建大型应用程序的形式主义工具。它们根本没有必要。
实际上有一个有趣的 post 叫做 You might not need redux by Dan Abramov,redux 的创建者可能会给你比我更好的答案:https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367#.7093fm1z8
如果状态不需要与其他组件共享,或者组件卸载时不需要保留状态,则可以将其放在组件的状态中。
你可以认为 Redux store 是前端的数据库,如果你有从 API 中获取产品数据之类的东西,那么 Redux store 就是正确的地方;如果你有一个下拉组件,它需要一个 isOpen
属性,那么该下拉列表的父级可以保持 dropdownIsOpen
作为组件状态。
更多信息,这里是丹的回答:https://github.com/reactjs/redux/issues/1287
你也说
only 1 container component and the rest of it as stateless component
这是不正确的。您可以有多个容器组件。一个容器组件还可以包含另一个容器组件。
来自书本:
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.
如果只有特定组件需要状态,那么更喜欢使用反应状态。 例如使用 UI 动作
的状态如果您想在 component/project 中使用任何状态,请使用 redux 状态。 例如API响应数据等
有关详细信息,请参阅此 link
https://spin.atomicobject.com/2017/06/07/react-state-vs-redux-state/