假设您从数据库中获取数据 - 最好是使用动作创建者:
dispatch(fetchDataStart());
//then on success
dispatch(fetchDataSuccess(data));
然后让reducer部分看起来像:
case FETCH_DATA_START:
return { ...state, isFetching:true };
case FETCH_DATA_SUCCESS:
return { ...state, isFetching:false, data:action.data };
或者将获取逻辑与数据逻辑分开并做以下事情更好:
dispatch(fetchDataStart());
//then on success
dispatch(fetchDataFinish());
dispatch(updateData(data));
减速器:
case FETCH_DATA_START:
return { ...state, isFetching:true };
case FETCH_DATA_FINISH:
return { ...state, isFetching:false };
case UPDATE_DATA:
return { ...state, data:action.data };