您好,我对 React、Redux 和 Saga 还很陌生。所以我有一个场景,我有一个 .jsx 文件,它是视图文件,然后是一个用于调度的操作文件,我还使用 saga 来更新减速器中的数据。以下是文件结构:
- 动作文件:
export const getAction = (requestor) => ({
type: GET_ACTION,
data: {
requestor,
},
});
- 减速机文件
export const Reducer = (currentState = {}, action) => {
const newState = { ...currentState };
switch (action.type) {
case GET_ACTION:
newState.data = action.data;
return newState;
}
};
- 佐贺文件
function* getData(action) {
const { requestor } = action.data;
try {
const data = yield call(callService);
if(success) {
yield put( {type: GET_ACTION, data} );
}
} catch (e)
{
}
}
function* getDataSaga() {
yield takeLatest(GET_ACTION, getData);
}
export {
getData,
};
export default [
getDataSaga,
];
- .jsx 文件
const [dummy, setDummy] = useState([]);
const data = useSelector(state => state.data, shallowEqual) || {};
There is a function in which dispatch function is called.
dispatch(getAction(requestor));
现在我需要在调度完成更新数据后访问数据的更新状态,因为在数据更新后我必须 setDummy 来设置提到的虚拟变量。我可以采取的任何方式来实现这一目标。我曾尝试使用 dispatch.then 但在 UI 上它说 .then 不是调度函数。