immutable.js在react项目中的简单使用

37次阅读

共计 730 个字符,预计需要花费 2 分钟才能阅读完成。

immutable.js
immutable 可以将一个对象转为一个不可更改的对象。对 Immutable 对象的任何修改或添加删除操作都会返回一个新的 Immutable 对象。
fromJS
header/store/reducer.js
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;
}
};
index.js—– 组件
const mapStateToProps = (state) => {
return {
// focused: state.get(“header”).get(“focused”)
focused: state.getIn([‘header’, ‘data’])
}
};
store/reducer.js
import {combineReducers} from “redux-immutable”;
import {reducer as headerReducer} from ‘../common/header/store’;

const reducer = combineReducers({
header: headerReducer
});

export default reducer;

正文完
 0