在 action-types.js 中定义
// 获取提现记录
export const GETCASHLIST = 'GETCASHLIST'
在action.js中定义action
// 获取提现记录列表,保存至redux
export const getCashList = () => {
return async dispatch => {
try{
const value = await API.getCashList();
dispatch({
type: GETCASHLIST,
value,
initLoading:false
})
}catch(err){
console.error(err);
}
}
}
在reducer.js中根据类型返回
export const cashInfo = (state = defaultState , action = {}) => {
switch(action.type){
case GETCASHLIST:
state.cashList = [...action.value]
state.initLoading = action.initLoading
return {...state}
case ADDTOCASHLIST:
state.cashList.unshift(action.value);
return state;
case RESETUSEMONEY:
state.usefulMoney = action.value;
return state;
default:
return state;
}
}
上述reducer。js中 case GETCASHLIST: return state 无法更新视图 return {...state} 视图就可以更新
想问一下是为什么
不要直接修改state,而是返回新的state: