什么时候选择React状态与Redux Store [英] When do I choose React state Vs Redux Store

查看:98
本文介绍了什么时候选择React状态与Redux Store的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习Redux,但我不清楚的一部分是,如何确定使用React State与Redux Store之间的关系,然后分派动作.从到目前为止的阅读来看,看起来我可以使用React状态代替Redux存储,并且仍然可以完成工作.我理解使用Redux存储并仅拥有1个容器组件,其余的作为无状态组件的关注点分离,但是我如何确定何时使用React state Vs redux存储的决定对我来说不是很清楚.有人可以帮忙吗?

I've been learning Redux and a part I'm unclear of is, how do I make a determination between using react state vs redux store and then dispatching actions. from my reading so far it looks like I could use React state in place of Redux store and still get things done. I understand the separation of concerns with using Redux store and just having 1 container component and the rest of it as stateless component but how do I make the determination of when to use React state Vs redux store is not very clear to me. Can someone please help?

谢谢!

推荐答案

如果不需要与其他组件共享状态,或者在卸载组件时不需要保持状态,则可以只需将其置于组件的状态即可.

If the state doesn't need to be shared with other components, or the state doesn't need to be keep when the component is unmounted, then you can just put it in the component's state.

您可以认为Redux存储是前端数据库,如果您有从API提取的产品数据之类的东西,那么Redux存储就是正确的位置;如果您有一个使用isOpen道具的下拉组件,则该下拉列表的父级可以仅将dropdownIsOpen保留为组件状态.

You can think that the Redux store is the database of front-end, if you have something like product data fetched from an API, then the Redux store is the right place; if you have a dropdown component, which takes a isOpen prop, then the parent of that dropdown can just keep dropdownIsOpen as a component state.

有关更多信息,这是Dan的答案: https://github.com/reactjs/redux/issues/1287

For more information, here is the answer from Dan: https://github.com/reactjs/redux/issues/1287

您也说过

只有1个容器组件,其余为无状态组件

only 1 container component and the rest of it as stateless component

这是不正确的.您可以具有多个容器组件.一个容器组件也可以包含另一个容器组件.

This is incorrect. You can have several container components. A container component can also contain another container component.

这篇关于什么时候选择React状态与Redux Store的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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