我有一个情况,我需要分派和行动,并调用一个API,一旦API返回结果,我想显示模态或ridirect,如果API结果长度为1,我需要ridirect用户到另一个,否则,我需要显示模态在同一屏幕上,现在我想实现这使用回调,或者如果你有另一个更好的方法,请建议。
//分派动作
const getStores = (store) => {
const obj = {
storeId: store._id,
areaId:store.areadId
};
dispatch(StoreAction.getStores(obj));
};
//切片代码
getAreaStores: (state) => {
state.loading = true;
state.error = null;
state.stores = [];
},
getAreaStoresSuccess: (state, action) => {
state.loading = false;
state.stores = action.payload;
state.error = null;
},
getAreaStoresFailure: (state, action) => {
state.loading = false;
state.error = action.payload;
},
// Saga 代码
function* getStores({ payload }) {
try {
const response = yield call(API.getStores, payload);
if (response.success == "1") {
yield put(StoreAction.getAreaStoresSuccess(response.data));
}
} else {
yield put(StoreAction.getAreaStoresFailure(response.error));
}
} catch (error) {
yield put(StoreAction.getAreaStoresFailure(error));
}
}
1条答案
按热度按时间xt0899hw1#
我们不会在redux actions/sagas中传递回调。一旦你从API得到响应,你的组件就会监听存储区,更新存储区中的值,一旦数据在存储区中,你的组件就会自动得到通知,你会检查数据的长度,以显示模态或重定向。