【问题标题】:How to pass arguments to createAsyncThunk?如何将参数传递给 createAsyncThunk?
【发布时间】:2021-02-26 18:28:51
【问题描述】:

Accordingly to documentation 我可以将一些参数发送到 createAsyncThunk

export const someCustomAsyncThunk = createAsyncThunk(
    'counter/customFetch',
    async (someParam, thunkAPI) => { //<-- someParam ?
        const response = await fetch('https://someapi');

        return await response.json();
    }
);
extraReducers: {
   [someCustomAsyncThunk.fulfilled.type]: (state, action) => {
     // state dispatching gonna be there
   },

我在渲染中的某处有一个动作:

onClick={() => dispatch(someCustomAsyncThunk(38746))}

但我收到错误消息:Expected 0 arguments, but got 1.

我错过了什么?

【问题讨论】:

    标签: reactjs redux react-redux redux-toolkit


    【解决方案1】:

    解决方案并不明显,但总的来说,只需要为参数提供类型:

    export const someCustomAsyncThunk = createAsyncThunk(
        'counter/customFetch',
        async (someParam: number, thunkAPI) => { <-- put types here
    

    【讨论】:

      猜你喜欢
      • 2021-02-20
      • 2020-10-09
      • 2012-12-21
      • 2012-01-07
      • 2014-05-03
      • 2015-06-26
      • 2012-01-18
      • 2017-07-14
      • 2018-03-08
      相关资源
      最近更新 更多