switch(action.type) {
case INCREASE:
return Object.assign({}, state, { number: state.number + action.amount});
case DECREASE:
return Object.assign({}, state, { number: state.number - action.amount});
default:
return state;
A组件中:
connect方法:
const Main = connect(state => {
let { Count } = state;
return {
count: Count.number
}, action['Update'])(Task); // 连接redux
定义增加函数,改变number
increase = () => {
this.props.increase(1);
B组件中:
const Main = connect(state => {
let { Count } = state;
return {
count: Count.number
}, action['Update'])(Ipieces); // 连接redux
初次进入组件,获取到number是改变之后的,但浏览器一刷新,number就变成初始值了。
急求帮忙!!!!!!
用缓存将数据存起来,然后componentDidMount判断props上的数据长度是否是0来确定redux是否被清空或者刚初始化。
若为0,则获取缓存数据,从新dispatch一次set action即可
componentDidMount() {
//判断是否页面有被人为刷新,将导致redux stroe清空,需要从缓存中恢复store
let data = JSON.parse(localStorage.getItem('projectList'))
//我这里用size===0 是因为我的redux数据都用了immutable data
if(this.props.projectList.size === 0 && data) {
this.props.setProjectList(data)
浙ICP备 15005796号-2
浙公网安备 33010602002000号
CDN 存储服务由 又拍云 赞助提供