如果使用Redux和React-Redux,在React组件中是否需要状态? [英] No need for state in React components if using Redux and React-Redux?

查看:161
本文介绍了如果使用Redux和React-Redux,在React组件中是否需要状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅使用React管理状态

我知道如果您只使用React创建应用程序,您将最终管理所有您所创建的不同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和Redux管理状态

如果决定将Redux与React结合使用,则可以将每个React组件的所有状态移动到整个Redux应用程序状态。每个需要一片Redux应用程序状态的组件可以通过React-Redux的 connect 函数挂钩到该状态。

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. Each component that requires a slice of the Redux application state can then hook into the state via React-Redux's connect function.

问题

这是否意味着您不再需要编写任何处理React的州的React组件(即 this.setState )因为React-Redux是连接使用Redux的React组件通过将数据传递到容器组件中的状态为 props

Does this mean that you no longer need to write any React components that deal with React's state (i.e. this.setState) since React-Redux is connecting the React components with Redux state by passing data into the container component as props?

推荐答案

对此有不同的看法,但一般的看法似乎是redux应该只包含应用程序状态。个别反应组件如下拉列表或模态仍然会有自己的状态。

There are different opinions on this, but the general view seems to be that redux should only contain "application state". Individual react components like dropdowns or modals will still have their own state.

关于此问题仍有很多争论,请查看此问题,例如有关如何管理本地组件状态: https://github.com/reactjs/redux/issues/159

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

有些项目正在试图解决这个问题:

Some projects have been popping up that are trying to solve this "problem":

  • redux-react-local
  • recompose withReducer (more for reducer semantics for local, not global state)

这篇关于如果使用Redux和React-Redux,在React组件中是否需要状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆