我有一个场景,我有一个登录页面 Page-X,在该页面上,组件 x1 从 fetch API 调用中获取数据,我已在componentDidMount()
method 中处理了该数据,并在获取数据之前显示加载 gif。
现在,当我在 Page-X 上单击组件 y1 时,会打开一个模态,然后关闭模态,加载 gif 会返回组件 x1 ,并且看起来 redux 存储已从我获取 x1 的获取数据的位置清除( allallBatchesFromDB - 见下文)?那么当我从模态返回时是否需要再次进行 api 调用?如果是,我应该使用哪种生命周期方法?TIA
componentDidMount = () => {
store.dispatch( getDataDBNew(this.props.bugDetails.agentName, this.props.bugDetails.bugId, true,
true, true, false, 'PFM'));
console.log("callsed for "+this.props.bugDetails.bugId);
}
渲染部分:-
let allallBatchesFromDB = store.getState().batchFromDBReducer.batches;
(allallBatchesFromDB) ?
<div>
<Componentx1 />
</div>
:
<img width={40} height={40} src={loadinglinear}/>
}
拿来 :
export function getDataDBNew(agentName,bugId,isRegressionChecked,
isSegmentWiseChecked,isFeatureWiseChecked,
isDirectCallRequired,featureName){
return (dispatch)=>{
axios.post('apiurl', {
"agentName": agentName,
"bugId": bugId,
"isRegressionChecked": isRegressionChecked,
"isSegmentWiseChecked": isSegmentWiseChecked,
"isFeatureWiseChecked": isFeatureWiseChecked,
"isDirectCallRequired": isDirectCallRequired,
"featureName": featureName,
"isDBCall" : true })
.then(function (response) {
console.log("got batch new reponse from db"+ JSON.stringify(response));
return dispatch(fetchBatchFromDBNew(bugId,response));
})
.catch(function (error) {
console.log("getting errrorooror"+error);
});
}
}
export function fetchBatchFromDBNew(bugId,response){
return {
type : ACTIONS_TYPES.FETCH_ALL_BATCH_FROM_DB_ALL_BUGS,
batches : response.data,
bugId : bugId
};
}
减速机:
import {ACTIONS_TYPES} from '../Actions/action';
function batchFromDBReducer(state={batches:{},allBatchesFromDB:{},regressionBatchFromDB:{},segmentBatchFromDB:{},featureBatchFromDB:{}},action){
var bool =false ;
if(action.allBatchesFromDB){
bool = true ;
}
switch(action.type){
case ACTIONS_TYPES.FETCH_ALL_BATCH_FROM_DB_ALL_BUGS:
return {
batches: {
...state.batches,
[action.bugId]: action.batches
}
}
default:
return {
defaultBatchFromDB : state.regressionBatchFromDB
}
}
}
export default batchFromDBReducer;
最新编辑我的构造函数中还有以下代码行:
store.subscribe(() => this.forceUpdate());
除了FETCH_ALL_BATCH_FROM_DB_ALL_BUGS
在此期间调度的任何其他 redux 操作之外?如果是这样,这将清除先前获取的数据,因为batchFromDBReducer
reducer 中的默认处理程序不会扩展到先前的状态。使用以下内容更新默认句柄,看看是否能解决问题:
return {...state, defaultBatchFromDB : state.regressionBatchFromDB}
如果减速器打算以这种方式运行,那么在模态关闭时,检查导致 pageX 中重新渲染的原因。如果是 prop 更改,您可以在componentWillReceiveProps
. 如果是状态更改,请执行相同操作componentDidUpdate
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句