使用redux时应该使用this.setState()吗? [英] Should you ever use this.setState() when using redux?

查看:207
本文介绍了使用redux时应该使用this.setState()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用redux时应该使用this.setState()吗?还是应该一直派遣行动并依靠道具?

Should you ever use this.setState() when using redux? Or should you always be dispatching actions and relying on props?

推荐答案

setState的明确用法适用于具有本地显示状态但与全局应用程序无关的UI组件.例如,表示特定下拉菜单是否处于活动状态的布尔值不需要处于全局状态,因此可以更方便地由菜单组件的状态进行控制.

Clear uses of setState would be for UI components that have local display state, but aren't relevant for the global application. For example a boolean that represents whether a specific dropdown menu is actively displayed doesn't need to be in global state, so it's more conveniently controlled by the menu component's state.

其他示例可能包括层次结构的手风琴显示中线条的折叠/展开状态.或选项卡导航中当前选择的选项卡.但是,在这两个示例中,您仍可能选择全局处理UI状态.例如,如果您想将扩展/折叠状态保存在浏览器存储中,以便通过页面刷新来保存它,则将是必需的.

Other examples might include the collapse/expand state of lines in an accordion display of a hierarchy. Or possibly the currently selected tab in tab navigation. However in both of these examples you might still choose to handle UI state globally. For example this would be necessary if you wanted to persist the expand/collapse state in browser storage so that it would be preserved by page refresh.

实际上,通常最简单的方法是使用局部状态实现此类UI元素,然后根据需要将其重构为全局状态.

In practice it's usually easiest to implement such UI elements with local state, and refactor them into global state as needed.

这篇关于使用redux时应该使用this.setState()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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