React-Redux在2023年是否仍然有用?

huangapple go评论48阅读模式
英文:

Is React-Redux still useful as of 2023?

问题

为什么在React中使用react-redux,当我们已经有像useContext和useReducer这样的钩子。在使用Redux与React并存时,有哪些不同于React本身的进一步优势,以及这些优势有多值得使用?

英文:

Why use react-redux, when we have hooks such as useContext and useReducer in React.
What are the further advantages to using Redux alongside react, which are not present in react itself - and how worth are those advantages?

答案1

得分: 1

如果您有全局的useContext和/或useReducer,那么使用该上下文的所有组件都将重新渲染,无论它们使用的状态部分是否已更改,如果您还有一个未包装在useCallback中的处理程序,那么虚拟DOM比较将失败,并且这些组件将导致DOM更新。您可以拥有多个小型上下文,但这样您就会有多个状态来源(具体好坏取决于您的需求和实现方式)。

React可以与您自己的observable和useSyncExternalStore一起使用,它可以包含全局存储的一部分或所有组件的一个状态。通过使用选择器,它可以防止重新渲染那些未更改状态的组件。

如果您不想编写自己的observable,那么您可以使用Redux,并将其与React连接到react-redux。

英文:

If you have a global useContext and/or/with useReducer then all components using that context will re render no matter if the part of the sate they use has changed or not, if you also have a handler that is not wrapped in useCallback then virtual DOM compare will fail and these component will cause DOM updates. You can have multiple small contexts but then you have multiple sources of your state (could be good or bad depending on your needs and implementation).

React could be used with your own observable and useSyncExternalStore that has parts of global store or just one state for all components. With selectors it would prevent components from re rendering that did not have changed state.

If you don't want to write your own observable then you can just use Redux and connect it to React with react-redux.

huangapple
  • 本文由 发表于 2023年2月10日 03:17:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定