在 react redux 和 saga 中完成调度后获取状态变量?

Get the state variable after dispatch is finished in react redux and saga?

你好,我是 React、Redux 和 Saga 的新手。所以我有一个场景,我有一个 .jsx 文件,它是视图文件,然后是一个用于调度的操作文件,我还使用 saga 来更新减速器中的数据。以下是文件结构:

  1. 动作文件:
export const getAction = (requestor) => ({
  type: GET_ACTION,
  data: {
    requestor,
  },
});
  1. 减速器文件
export const Reducer = (currentState = {}, action) => {
  const newState = { ...currentState };

  switch (action.type) {
    case GET_ACTION:
      newState.data = action.data;
      return newState;
  }  
};
  1. 传奇文件
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,
];
  1. 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 不是调度函数。

after the data is updated I have to setDummy

useEffect 让你可以根据给定的道具改变做一些事情

const [dummy, setDummy] = useState([]);
const data = useSelector(state => state.data, shallowEqual) || {};

// setDummy when `data` changes
useEffect(() => {
  setDummy(data);
}, [data])