使用不可变 JS 映射时如何设置多个对象值
How to set multiple object values when using Immutable JS map
我是 redux 的新手,请问在下面的代码中这样做是否正确?
这是调用执行 currentTime 的 action 时的 reducer 方法。
import { combineReducers } from 'redux';
import { UPDATE_TIME } from './actions';
import { Map } from 'immutable';
const initialState = Map({update:false, currentTime: ""});
function currentTime(state = initialState, action) {
switch (action.type) {
case UPDATE_TIME:
return {...state, update: true, currentTime: action.time };
default:
return state;
}
}
const currentTimeReducer = combineReducers({
currentTime
});
export default currentTimeReducer
我们使用不可变 JS 在现有对象的每个小变化上创建新实例。不可变 JS MAP 有一个 set
方法来设置属性和 return 对象的新实例。
Here 您可以找到 api MAP 文档
import { combineReducers } from 'redux';
import { UPDATE_TIME } from './actions';
import { Map } from 'immutable';
const initialState = Map({update:false, currentTime: ""});
function currentTime(state = initialState, action) {
switch (action.type) {
case UPDATE_TIME:
let newState = state;
newState = newState.set('update', true );
newState = newState.set('currentTime', action.time);
return newState;
default:
return state;
}
}
const currentTimeReducer = combineReducers({
currentTime
});
export default currentTimeReducer
在 this 文档中查看最佳实践
有多种方法可以实现
您可以使用 set()
函数设置值
case UPDATE_TIME:
state = state.set('update', true);
return state.set('currentTime', action.time);
甚至
case UPDATE_TIME:
return state.set('update', true)
.set('currentTime', action.time);
但是,当您有多个更改时,这是不可行的
另一个选项是merge()
case UPDATE_TIME:
return state.merge({update: true, currentTime: action.time})
但是,在嵌套状态更新的情况下,您需要执行深度合并。详见mergeDeep
我是 redux 的新手,请问在下面的代码中这样做是否正确? 这是调用执行 currentTime 的 action 时的 reducer 方法。
import { combineReducers } from 'redux';
import { UPDATE_TIME } from './actions';
import { Map } from 'immutable';
const initialState = Map({update:false, currentTime: ""});
function currentTime(state = initialState, action) {
switch (action.type) {
case UPDATE_TIME:
return {...state, update: true, currentTime: action.time };
default:
return state;
}
}
const currentTimeReducer = combineReducers({
currentTime
});
export default currentTimeReducer
我们使用不可变 JS 在现有对象的每个小变化上创建新实例。不可变 JS MAP 有一个 set
方法来设置属性和 return 对象的新实例。
Here 您可以找到 api MAP 文档
import { combineReducers } from 'redux';
import { UPDATE_TIME } from './actions';
import { Map } from 'immutable';
const initialState = Map({update:false, currentTime: ""});
function currentTime(state = initialState, action) {
switch (action.type) {
case UPDATE_TIME:
let newState = state;
newState = newState.set('update', true );
newState = newState.set('currentTime', action.time);
return newState;
default:
return state;
}
}
const currentTimeReducer = combineReducers({
currentTime
});
export default currentTimeReducer
在 this 文档中查看最佳实践
有多种方法可以实现
您可以使用
set()
函数设置值case UPDATE_TIME: state = state.set('update', true); return state.set('currentTime', action.time);
甚至
case UPDATE_TIME: return state.set('update', true) .set('currentTime', action.time);
但是,当您有多个更改时,这是不可行的
另一个选项是
merge()
case UPDATE_TIME: return state.merge({update: true, currentTime: action.time})
但是,在嵌套状态更新的情况下,您需要执行深度合并。详见mergeDeep