【问题标题】:redux toolkit createAsyncThunk typescript errorsredux 工具包 createAsyncThunk 打字稿错误
【发布时间】:2021-11-01 02:12:54
【问题描述】:

我正在尝试使用 @reduxjs/toolkit 和 typescript 调用 API 端点。

我按照这里的例子,没有成功https://redux-toolkit.js.org/api/createAsyncThunk

我遇到了一些打字错误,我做错了什么?

1°error:当我像在 MessageController.tsx 中那样进行调度时,打字稿说: “AsyncThunk”类型的参数不能分配给“AnyAction”类型的参数。 'AsyncThunk' 类型中缺少属性 'type' 但在 'AnyAction'.ts(2345) 类型中是必需的 index.d.ts(19, 3): 'type' 在这里声明。

2° 错误:在 message.slice.ts 中,每当我执行 message.fulfilled 或 denied 或 pending 时,我都会得到: 类型 '(data: any) => AsyncThunk'.ts(2339)

上不存在属性 'fulfilled'

message.slice.ts

export const sendMessage = ( data) =>createAsyncThunk(
    `message/sendMessage`,
    async (data, thunkAPI) => {
        const response = await axios.post
        ( `/api/v1/xxx/test-messages`,
        data,
        httpOptions(getDefaultHeaders()));

       return response.data;
      }
    )
    
    
    
    const messageSlice = createSlice({
    name: "message",
    initialState: {
       message: {},
       loading: "idle",
       error: "",
    },
    reducers: {},
    extraReducers: (builder) => {
       builder.addCase(sendMessage.pending, (state) => {
         state.message = {};
           state.loading = "loading";
       });
       builder.addCase(
        sendMessage.fulfilled, (state, { payload }) => {
             state.message = payload;
             state.loading = "loaded";
       });
       builder.addCase(
        sendMessage.rejected,(state, action) => {
             state.loading = "error";
             state.error = action.error.message;
       });
    }
  })
  
  interface stateI{
  message: messageI;
}
interface messageI{
  loading: string;
}

  export const selectMessage = createSelector(
    (state: stateI) => ({
       message: state.message,
       loading: state.message.loading,
    }), (state) =>  state
  );

MessageController.tsx


  import {useAppDispatch} from '../../state/store';

  const dispatch = useAppDispatch();

  const { message } = useSelector(selectMessage);
    React.useEffect(() => {
     console.log(message)
     // TODO display the result of the api call in the page..
  }, [message]); 
  
  const handleClick =()=>{
    const data = {
      "environment": dropboxValue,
      "payload": textareaValue
    };

    dispatch(sendMessage(data))
    .then(() => {
      // TODO display the result of the api call in the page..
    })

  }
  
   //additional ui code for a button which when clicked trigger the api call (sendMessage)


store.ts

export type AppDispatch = typeof store.dispatch;
export const useAppDispatch = () => useDispatch<AppDispatch>();
//other code not relevant (I think)

【问题讨论】:

    标签: reactjs typescript redux async-await redux-toolkit


    【解决方案1】:

    案例1:你创建useAppDispatch错了。更新可以这样:

    export const useAppDispatch = useDispatch<AppDispatch>();
    

    案例 2:您创建了错误的 thunk 操作。你可以这样更新:

    export const sendMessage = createAsyncThunk(
        `message/sendMessage`,
        async (data, thunkAPI) => {
        ...
    

    【讨论】:

    • 谢谢,我没有第二个错误了,但是调度调用的第一个错误在另外两个错误中发生了变化:1)这个表达式是不可调用的。类型 'AnyAction' 没有调用 signatures.ts(2349) 2) 预期 0 个参数,但得到 1.ts(2554)
    • the 2) 实际上对我有意义..我需要将数据传递给 sendMessage() 但您建议在没有数据参数的情况下编写签名
    • 你对#2 是对的,但对#1 是错的。他们说得对。他们正在创建一个新的钩子,所以它需要是一个函数。
    猜你喜欢
    • 2022-07-28
    • 2021-08-24
    • 2021-12-18
    • 2021-11-06
    • 2021-06-03
    • 2021-02-20
    • 2018-05-23
    • 1970-01-01
    • 2018-06-30
    相关资源
    最近更新 更多