Redux 和不可变
Redux and immutables
我正在尝试在 reducer 函数中添加不可变。但是,当来自 firebase 的数据命中时,我收到错误 Uncaught TypeError: currentState.merge is not a function
。
reducer代码如下(注释代码不可变)
import C from '../../constants';
import initialState from '../initialState';
import {Map, List, fromJS} from 'immutable';
const membersInitialState = fromJS(initialState.members);
export default (currentState = membersInitialState, action) => {
let newstate;
switch (action.type) {
case C.RECEIVE_MEMBER_DATA:
/*return Object.assign({}, currentState, {
hasReceivedData: true,
data: action.data
});*/
return currentState.merge({
hasReceivedData: true,
data: action.data
});
//default: return currentState || initialState.members;
}
return currentState;
}
rootReducer 如下
import membersReducer from './members';
function rootReducer (state, action){
return {
members: membersReducer(state.members, action)
}
};
export default rootReducer;
问题是因为 store
创建时没有使用不可变对象。
export default createStoreWithMiddleware(rootReducer, fromJS(initialState));
如上更新后,可以正常使用了。
我正在尝试在 reducer 函数中添加不可变。但是,当来自 firebase 的数据命中时,我收到错误 Uncaught TypeError: currentState.merge is not a function
。
reducer代码如下(注释代码不可变)
import C from '../../constants';
import initialState from '../initialState';
import {Map, List, fromJS} from 'immutable';
const membersInitialState = fromJS(initialState.members);
export default (currentState = membersInitialState, action) => {
let newstate;
switch (action.type) {
case C.RECEIVE_MEMBER_DATA:
/*return Object.assign({}, currentState, {
hasReceivedData: true,
data: action.data
});*/
return currentState.merge({
hasReceivedData: true,
data: action.data
});
//default: return currentState || initialState.members;
}
return currentState;
}
rootReducer 如下
import membersReducer from './members';
function rootReducer (state, action){
return {
members: membersReducer(state.members, action)
}
};
export default rootReducer;
问题是因为 store
创建时没有使用不可变对象。
export default createStoreWithMiddleware(rootReducer, fromJS(initialState));
如上更新后,可以正常使用了。