简体   繁体   English

如果使用Redux和React-Redux,在React组件中是否需要状态?

[英]No need for state in React components if using Redux and React-Redux?

Managing state with React only 仅使用React管理状态

I understand that if you're creating an application using React only, you will end up managing all of your state within different React components you create. 我了解如果您只使用React创建应用程序,您将最终管理您创建的不同React组件中的所有状态。

Managing state with React and Redux 使用React和Redux管理状态

If you decide to use Redux in combination with React, you can then move all of the state from each of your React components into the overall Redux application state. 如果您决定将Redux与React结合使用,则可以将每个React组件的所有状态移动到整个Redux应用程序状态。 Each component that requires a slice of the Redux application state can then hook into the state via React-Redux's connect function. 然后,需要切片Redux应用程序状态的每个组件都可以通过React-Redux的connect函数挂钩到状态。

Question

Does this mean that you no longer need to write any React components that deal with React's state (ie this.setState ) since React-Redux is connect ing the React components with Redux state by passing data into the container component as props ? 这是否意味着您不再需要编写任何处理React state React组件(即this.setState ),因为React-Redux通过将数据作为props传递到container组件来将React组件与Redux状态connect

There are different opinions on this, but the general view seems to be that redux should only contain "application state". 对此有不同的看法,但一般的看法似乎是redux应该只包含“应用程序状态”。 Individual react components like dropdowns or modals will still have their own state. 单个反应组件如下拉或模态仍将具有自己的状态。

There is still a lot of debate on this though, check out this issue for example about how to manage local component state: https://github.com/reactjs/redux/issues/159 关于这一点仍有很多争论,请查看此问题,例如关于如何管理本地组件状态: https//github.com/reactjs/redux/issues/159

Some projects have been popping up that are trying to solve this "problem": 有些项目正在试图解决这个“问题”:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM