本文介绍了在Redux中重写状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在redux中,我了解状态是不可变的,当您创建新状态时,您实质上是使用已有的新信息来更新对象,然后完全重写状态.

In redux I understand that state is immutable and when you create new state you are essentially updating the object with what ever new information there is and then totally rewriting the state.

今天我有一个想法,但不确定它有多愚蠢.

Today I had a thought and I am not sure how stupid it is.

继续重写状态在计算上昂贵吗?我知道这是Redux的主要范例之一,但是我想知道从内存和空间的角度来看这是否有意义.

Is it computationally expensive to keep re-writing the state? I know that this is one of the major paradigms of Redux, but I want to know if this makes sense from a memory and space perspective.

推荐答案

您可以在Redux中对状态进行突变,但是您不应该为此付出任何代价,因为您将在 Redux反模式

You are allowed to mutate the state in Redux but you should not do it at any cost because you'd be coding in Redux anti-patterns

在原始JavaScript或任何框架中,对象的更改可能会带来许多副作用,这可能会给调试带来很大的痛苦.您应该选择纯函数,除非有必要进行突变.

Mutating objects, in vanilla JavaScript or in any framework, may bring many side-effects with it which could be very painful to debug. You should opt for pure functions unless its necessary to mutate.

现在回到Redux,Reducer中的功能应该是纯功能.这是为什么:

Now back to Redux, functions in reducers should be pure functions. here is why:

Redux算法检查通过比较上一个和下一个状态的存储位置来更新状态.

Redux algorithm checks if a state has been updated by comparing the memory location of the previous and the next state.

现在,当您使用JavaScript更改对象时,您只是在更新现有对象,因此,内存位置保持不变,并且存储也不会更新.更改状态还会禁用 Redux devtools 的一项基本功能,这会耗时进行调试.

Now, when you mutate an object in JavaScript, you are simply updating an existing object and therefore, the memory location remains the same and the store does not get updated. Mutating the state also disables an essential feature of Redux devtools, time-travelling to debug.

另一方面,如果不是突变对象,而是创建一个新对象,则当redux比较previousState(更改前的状态)和nextState(您发送的新对象)的内存位置时,Redux会在这一点意识到已经发生了变化,并以您的最新状态更新了商店.

On the other side, if instead of mutating the object you create a new one, when redux compares the memory location of previousState (the state before you changed it) and the nextState(the new one which you sent), Redux at this point realises that there has been a change and it updates the store with you latest state.

参考:

  • Redux anti-patterns:https://github.com/coodoo/react-redux-isomorphic-example/issues/9

Redux devtools的功能不正确和问题:
https://github.com/coodoo/react- redux-isomorphic-example/commit/6998c46d3c1a102b5f1bfb4f9aa44e5e7f9f6e87#commitcomment-12457617

impure functions and issues with Redux devtools:
https://github.com/coodoo/react-redux-isomorphic-example/commit/6998c46d3c1a102b5f1bfb4f9aa44e5e7f9f6e87#commitcomment-12457617

什么是纯函数?
https://medium. com/javascript-scene/master-the-javascript-interview-what-is-a-pure-function-d1c076bec976

What is pure function?
https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-pure-function-d1c076bec976

为什么Redux需要减速器成为纯函数"
https://medium.freecodecamp.org/为什么redux需要减少器成为纯功能d438c58ae468

Why Redux need reducers to be "pure functions"
https://medium.freecodecamp.org/why-redux-needs-reducers-to-be-pure-functions-d438c58ae468

Redux开发工具: https://github.com/gaearon/redux-devtools

Redux devtools: https://github.com/gaearon/redux-devtools

这篇关于在Redux中重写状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:35