- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
Redux 应用程序中的初始状态可以通过两种方式设置:
createStore
( docs link ) 最佳答案
TL;DR
Without
combineReducers()
or similar manual code,initialState
always wins overstate = ...
in the reducer because thestate
passed to the reducer isinitialState
and is notundefined
, so the ES6 argument syntax doesn't get applied in this case.With
combineReducers()
the behavior is more nuanced. Those reducers whose state is specified ininitialState
will receive thatstate
. Other reducers will receiveundefined
and because of that will fall back to thestate = ...
default argument they specify.In general,
initialState
wins over the state specified by the reducer. This lets reducers specify initial data that makes sense to them as default arguments, but also allows loading existing data (fully or partially) when you're hydrating the store from some persistent storage or the server.
combineReducers()
.
function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT': return state + 1;
case 'DECREMENT': return state - 1;
default: return state;
}
}
现在假设您用它创建了一个商店。
import { createStore } from 'redux';
let store = createStore(counter);
console.log(store.getState()); // 0
初始状态为零。为什么?因为
createStore
的第二个参数是
undefined
.这是
state
第一次传递给你的 reducer 。当 Redux 初始化时,它会分派(dispatch)一个“虚拟” Action 来填充状态。所以你的
counter
使用
state
调用 reducer 等于
undefined
.
这正是“激活”默认参数的情况。 因此,
state
现在是
0
根据默认
state
值(
state = 0
)。将返回此状态 (
0
)。
import { createStore } from 'redux';
let store = createStore(counter, 42);
console.log(store.getState()); // 42
为什么是
42
, 而不是
0
, 这次?因为
createStore
被调用
42
作为第二个论点。该参数变为
state
与虚拟 Action 一起传递给您的 reducer 。
这一次,state
不是未定义的(它是 42
!),因此 ES6 默认参数语法无效。
state
是
42
, 和
42
从 reducer 返回。
combineReducers()
的情况。 .
function a(state = 'lol', action) {
return state;
}
function b(state = 'wat', action) {
return state;
}
combineReducers({ a, b })
生成的reducer看起来像这样:
// const combined = combineReducers({ a, b })
function combined(state = {}, action) {
return {
a: a(state.a, action),
b: b(state.b, action)
};
}
如果我们调用
createStore
没有
initialState
,它将初始化
state
至
{}
.因此,
state.a
和
state.b
将是
undefined
当它调用
a
时和
b
reducer 。
两个a
和 b
reducer 将收到 undefined
作为他们的state
参数,如果他们指定默认 state
值,那些将被返回。 这就是组合 reducer 返回
{ a: 'lol', b: 'wat' }
的方式。第一次调用时的状态对象。
import { createStore } from 'redux';
let store = createStore(combined);
console.log(store.getState()); // { a: 'lol', b: 'wat' }
让我们考虑一个不同的场景:
import { createStore } from 'redux';
let store = createStore(combined, { a: 'horse' });
console.log(store.getState()); // { a: 'horse', b: 'wat' }
现在我指定了
initialState
作为
createStore()
的参数.从组合 reducer 返回的状态结合了我为
a
指定的初始状态带
'wat'
的 reducer 默认参数指定
b
reducer 选择了自己。
// const combined = combineReducers({ a, b })
function combined(state = {}, action) {
return {
a: a(state.a, action),
b: b(state.b, action)
};
}
在这种情况下,
state
已指定,因此它不会回退到
{}
.它是一个带有
a
的对象字段等于
'horse'
,但没有
b
field 。这就是为什么
a
收到 reducer
'horse'
作为它的
state
并高兴地退回了它,但是
b
收到 reducer
undefined
作为它的
state
并因此返回了默认
state
的想法(在我们的示例中,
'wat'
)。这就是我们得到
{ a: 'horse', b: 'wat' }
的方式作为返回。
undefined
调用 reducer 时从 reducer 返回初始状态。作为
state
参数(实现这一点的最简单方法是指定
state
ES6 默认参数值),您将对组合 reducer 有一个很好的有用行为。
他们会更喜欢 initialState
中的相应值。传递给 createStore()
的对象函数,但是如果你没有传递任何,或者没有设置对应的字段,默认state
而是选择由 reducer 指定的参数。 这种方法效果很好,因为它提供了现有数据的初始化和水合,但是如果他们的数据没有被保留,它会让单个 reducer 重置他们的状态。当然,您可以递归地应用此模式,因为您可以使用
combineReducers()
在许多层面上,甚至通过调用 reducer 并为它们提供状态树的相关部分来手动组合 reducer。
关于javascript - 在 Redux Reducer 中读取 Store 的初始状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33749759/
我不知道如何从 reducerRegister.js 中的 reducerForm.js reducer 访问 bool 值 isLoading 标志。我使用了 combineReducers() 并
我正在尝试找到一种理想的方法来更新我的状态树上的几个顶级字段,同时仍然维护拆分 reducer 。 这是我想出的一个简单的解决方案。 var state = { fileOrder: [0],
如果我们想按相同的键(第一个 reducer 的输出)分组,是否可以将 reducer 的输出直接发送到另一个 reducer 有时在链接时我发现我正在使用一个映射器来读取输入并将其复制到输出。因此想
我有一个如下所示的 reducer : const chart = combineReducers({ data, fetchProgress, fetchError,
当Map Reduce代码中有多个reduce时,它们之间没有任何形式的通信。但是,当执行诸如聚合之类的操作时,所有化简器共同产生单个最终输出。当它们之间没有通信时,聚合如何发生?是通过写入上下文吗?
我在 hive 中有一个表,我想从中获取所有数据。问题是: select * from tbl; 给我的结果与以下情况截然不同: select count(*) from tbl; 这是为什么?第二个
假设我有一个带有两个 reducer 的应用程序 - 使用 combineReducers() 组合的 tables 和 footer。 当我点击某个按钮时,将分派(dispatch)两个操作 - 一
我正在学习更深入的 redux,并且在处理高阶 reducer 时遇到一些麻烦。 我试图使用一个简单的分页示例来了解它是如何工作的。 NB:下面的代码只是 Nodejs 上下文中 redux 的一个快
我调用 RSS 提要并使用解析器对其进行解析。我收到一个数组。我现在想在最后创建一个对象,看起来像这样: { "2019-06-13": { "rates": { "usd":
我有一份学生列表,我的应用程序始终显示当时的一个学生,即 activePupil。到目前为止我有两个 reducer 。其中一个包含并默认返回所有子项的列表(以数组的形式): [ { id:
我有一个叫做 animals 的特征缩减器(切片缩减器)。我想将这些 reducer 拆分为哺乳动物、鸟类、鱼类等。这部分很简单,因为我可以简单地使用 ActionReducerMap。 现在假设哺乳
空数组上的简单reduce会抛出: 线程“main”java.lang.UnsupportedOperationException 中的异常:无法减少空的可迭代对象。 链接时同样的异常: val a
我有一些 25k 文档(原始 json 中为 4 GB)的数据,我想对其执行一些 javascript 操作,以使我的最终数据使用者 (R) 更容易访问这些数据,并且我想通过为每个更改添加一个新集合来
我只是想验证我对这些参数及其关系的理解,如果我错了请通知我。 mapreduce.reduce.shuffle.input.buffer.percent 告诉分配给 reducer 的整个洗牌阶段的内
我想将 redux 状态的值从 reducer 传递到另一个 reducer。在我的例子中,我想将 groups 的值从 groupReducer.js 中的状态传递到 scheduleReducer
所以,我有一个应用程序,它有多个 reducer ,因此有多个关联的 Action 创建者。 有一段时间,我的一个 reducer 更新了状态(由于编辑),因此,我必须确保其他 reducer 看到此
我有一个 reducer ,可以在调度操作时重新调整适当的状态。现在我定期调用 API,因此结果会一次又一次地触发操作。所以我想要的是,如果 reducer 状态已经有数据,那么另一个 reducer
当我尝试执行来自 here 的 DISTINCT reduce 时,出现错误。我已经在啤酒 sample 桶上重现了这个错误,所以这应该很容易重现。我没有在 mapreduce_errors.txt
在以下语法的简单优先级解析(分解)中,我们存在 shift-reduce 和 reduce-reduce 冲突。 X 是开始符号,X'-->$X$ 是添加规则。另外+和下符号是终结符。 X'-->$X
我需要编写一个连续调用两个reducer的Mapreduce程序。即,第一个 reducer 的输出将是第二个 reducer 的输入。我如何实现这一目标? 到目前为止我发现的内容表明我需要在我的驱动
我是一名优秀的程序员,十分优秀!