immutable.js
immutable可以将一个对象转为一个不可更改的对象。对 Immutable 对象的任何修改或添加删除操作都会返回一个新的 Immutable 对象。
fromJS
import { fromJS } from 'immutable';
const defaultState = fromJS({
data: {},
list: []
})
export default (state = defaultState, action) => {
switch (action.type) {
case 'change_data':
return state.set("data", action.data);
case 'change_all':
return state.merge({
list: action.data,
data: action.data
});
default:
return state;
}
};
const mapStateToProps = (state) => {
return {
// focused: state.get("header").get("focused")
focused: state.getIn(['header', 'data'])
}
};
import { combineReducers } from "redux-immutable";
import { reducer as headerReducer } from '../common/header/store';
const reducer = combineReducers({
header: headerReducer
});
export default reducer;