Action.jsx
export const increase = n => {
return {
type: INCREASE,
amount: n
}
}
export const decrease = n => {
return {
type: DECREASE,
amount: n
}
}
Reducer.jsx
// 初始化state数据
const initialState = {
number: 1
};
const update = (state = initialState, action) => {
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就变成初始值了。
急求帮忙!!!!!!
不用疑惑,你是刷新浏览器,相当于重新开启应用,必定会从最初的状态重新开始!
要想保存值要么向后台发请求,做出真实的改变,要么将值本地存储,从本地存储去取值。
单说刷新浏览器,肯定是回复到最初状态,相当于什么也没做