1

您好,我对 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 不是调度函数。

4

1 回答 1

1

数据更新后我必须 setDummy

useEffect让你在给定的道具改变时做一些事情

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

// setDummy when `data` changes
useEffect(() => {
  setDummy(data);
}, [data])
于 2020-12-01T21:20:55.427 回答