immutable库和redux-immutable库使用
文档目录
immutable使用
1、安装:cnpm i immutable
在reducer中引用
import { fromJS } from 'immutable';
const defaultState = fromJS ({
focused : false
});
immutable对象set方法,会结合immutable对象的值,和设置的值,返回一个全新的对象
export default (state = defaultState, action)=>{
if(action.type === constants.SEARCH_FOCUS){
return state.set("focused",true);
}
if(action.type === constants.SEARCH_BLUR){
return state.set("focused",false);
}
return state;
}
2、把对象转为immutable对象变为不可更改
3、index.js中使用
const mapStateToProps = (state, ownProps) => {
return {
focused: state.header.get("focused")
}
}
redux-immutable统一index中的使用规则:
redux-immutable安装方法:cnpm i redux-immutable -D
使用方法:
import { combineReducers } from 'redux-immutable';
import { reducer as headerReducer } from '../common/header/store';
const reducer = combineReducers({
header: headerReducer
});
export default reducer;
index.js中使用redux-immutable中的getIn方法
const mapStateToProps = (state, ownProps) => {
return {
focused:state.getIn(['header','focused'])
}
}