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

查看:26
本文介绍了如果使用 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 的 state(即 this.setState)的 React 组件,因为 React-Redux 是 connect通过将数据作为 props 传递到 container 组件来使 React 组件具有 Redux 状态?

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 应该只包含应用程序状态".下拉菜单或模态等单独的 React 组件仍将拥有自己的状态.

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天全站免登陆