redux 我如何在“挂起”状态下发送自定义参数?

jslywgbw  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(84)

fetchUserById()被创建时,它的输出响应被定义。

const fetchUserById = createAsyncThunk<{user: UserInterface, id: number}, number>(
  'users/fetchByIdStatus',
  async (userId) => {
    const response = await userAPI.fetchById(userId)
    return {user: response.data, id: userId}
  }
);

const userSlice = createSlice({
  name: "users",
  initialState: [],
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase(fetchUserById.pending, (state, { payload }) => {
      // how I configured custom value for this payload?
      // I need to send the user id.
    });
    builder.addCase(fetchUserById.fulfilled, (state, {payload}) => {
      /**
       * @var {UserInterface} user
       * @var {number} id;
       */
      const {id, user} = payload;
      state[id].loading = false;
      state[id].user = user;
    });
  },
});

字符串
但是,当操作处于“挂起”状态时,我如何定义响应?

mnemlml8

mnemlml81#

传入createAsyncThunk的arg在action.meta.arg中也是可用的。所以在你的例子中,你可以像这样引用它:

builder.addCase(fetchUserById.pending, (state, { meta }) => {
      console.log(meta.arg); // meta.arg will have whatever value was passed as `userId`
    });

字符串

相关问题