问题描述
我有一个减速器,当一个动作被调度时,我正在重新调整适当的状态.现在我定期调用一个 API,所以结果会一次又一次地触发一个动作.所以我想要的是,如果reducer 状态已经有数据,那么另一个reducer 在发送调用时不会将状态显示为正在加载.只有第一次接收数据时,它必须保持其加载状态.希望我能解释清楚
这是我的代码片段
加载状态减速器
const loading = (state = false, action) =>{开关(动作.类型){case 'GET_AUDIT_DATA'://这里我只想在没有可用数据时返回 true返回真案例GET_AUDIT_DATA_RECEIVED":返回假案例GET_AUDIT_DATA_ERROR":返回假默认:返回状态}}
组合减速器
const allReducers = combineReducers({审计数据:审计数据,审计加载:审计加载,修改订单:修改订单});导出默认的 allReducers;
Reducer 返回有关超级代理触发的动作的数据
const auditData = (state = [], action) =>{开关(动作.类型){案例GET_AUDIT_DATA_RECEIVED":控制台日志(操作数据);返回 action.data;案例GET_AUDIT_DATA_ERROR":返回 action.err;默认 :返回状态;}}导出默认审计数据;
所以最初 auditData
不包含任何数据,只有在第一次成功调用后才返回数据.当同时调用加载状态reducer
时,它应该在GET_AUDIT_DATA
动作中返回true,只有当审计数据reducer不包含任何数据时.>
也只是从 auditData
返回当前获得的数据,这是正确的方法,否则我应该以不同的方式来做.基本上我想用 new one
来overwrite
current data
.
继续进行的最佳方式是向加载状态 reducer 发送信息,以了解其他 reducer 是否已经有数据.最后有:
const loading = (state = false, action) =>{开关(动作.类型){案例GET_AUDIT_DATA":if(!action.dataAlreadyInitialized){返回真}案例GET_AUDIT_DATA_RECEIVED":返回假案例GET_AUDIT_DATA_ERROR":返回假默认:返回状态}}
您应该可以从您的操作功能访问应用程序状态并执行以下操作:
调度({类型:'GET_AUDIT_DATA',dataAlreadyInitialized: appState.auditData.length >0});
I have a reducer whereby I am retuning the appropriate state when an action is dispatched. Now I am calling an API at regular intervals so the result will trigger an action again and again. So what I want is that if the reducer state already has data then another reducer doesn't show the state as loading while the call is sent. It must maintain its loading state when receiving data the first time only. I hope I am able to explain it properly
Here are my code snippets
Loading state reducer
const loading = (state = false, action) => {
switch (action.type) {
case 'GET_AUDIT_DATA': // here I want to return true only when there is no data available
return true
case 'GET_AUDIT_DATA_RECEIVED':
return false
case 'GET_AUDIT_DATA_ERROR':
return false
default:
return state
}
}
Combining reducers
const allReducers = combineReducers({
auditData: AuditData,
auditLoading: AuditLoading,
modifiedOrders: ModifiedOrders
});
export default allReducers;
Reducer returning data on action triggered by superagent
const auditData = (state = [], action) => {
switch(action.type) {
case 'GET_AUDIT_DATA_RECEIVED':
console.log(action.data);
return action.data;
case 'GET_AUDIT_DATA_ERROR':
return action.err;
default :
return state;
}
}
export default auditData;
So initially the auditData
doesn't contain any data, only after the first success call it returns the data. When this is called at the same time loading state reducer
is called and it should return true in GET_AUDIT_DATA
action only when the audit data reducer doesn't contain any data.
Also is returning just the current obtained data from auditData
the right way to go or I should do it differently. Basically I want to overwrite
the current data
with the new one
.
The best way to proceed is to send to Loading state reducer an information to know if the other reducer already have data. To have at the end:
const loading = (state = false, action) => {
switch (action.type) {
case 'GET_AUDIT_DATA':
if(!action.dataAlreadyInitialized){
return true
}
case 'GET_AUDIT_DATA_RECEIVED':
return false
case 'GET_AUDIT_DATA_ERROR':
return false
default:
return state
}
}
You should have access from your action function to the application state and do:
dispatch({
type:'GET_AUDIT_DATA',
dataAlreadyInitialized: appState.auditData.length > 0
});
这篇关于从另一个减速器中访问减速器状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!