简体   繁体   English

Redux工具包中如何设置typescript?

[英]How do I set up typescript in Redux toolkit?

I want to receive data from redux toolkit's createAsyncThunk when dispatching.我想在调度时从 redux 工具包的 createAsyncThunk 接收数据。

But I don't know how to set the data type.但是我不知道如何设置数据类型。

If no data type is specified, a warning line is displayed with a red line.如果未指定数据类型,则显示一条带有红线的警告线。

like this Screenshot喜欢这个截图

在此处输入图像描述

How do I specify the type?如何指定类型?

this is my code这是我的代码

    // commentinput is string and post.id is number

    const commetsubmitfun = useCallback(() => {
      dispatch(addComment({content: commentinput, postId: post.id}));
    }, [dispatch, commentinput]);




    export const addComment = createAsyncThunk(
      'post/addComment',
      async (data, {rejectWithValue}) => {
        try {
          //    data: {content: "aaa", postId: 66}
          const response = await axios.post(`/post/${data.postId}/comment`, data); // POST /post/1/comment
          return response.data;
        } catch (error: any) {
          return rejectWithValue(error.response.data);
        }
      },
    );

You should declare type when calling createAsyncThunk , declare an interface for data like this:您应该在调用createAsyncThunk时声明类型,为data声明一个接口,如下所示:

type DataType = { 
   content : string
   postId : string
}

then add it here:然后在此处添加:

      async (data : DataType, {rejectWithValue}) => {

You can read more here: https://redux-toolkit.js.org/usage/usage-with-typescript#createasyncthunk您可以在这里阅读更多内容: https://redux-toolkit.js.org/usage/usage-with-typescript#createasyncthunk

you specify the types in the createSlice file with either interface or type您使用接口或类型在 createSlice 文件中指定类型

eg:例如:

interface Action {
    type: string
    payload: {
       img: string
       images: string[] | [] | null
    }
}

type is always string, payload is what you put in the { } inside dispatch(addComment({content: ...payload, postId: ..payload})); type 始终是字符串,payload 是您在 { } 中放入的dispatch(addComment({content: ...payload, postId: ..payload}));

interface Action {
    type: string
    payload: {
       content: string
       postId: number //or if its possibly undefined/null postId?: number | null | undefined
    }
}

const initialStateValue = {
   post: null
}

reducers: {
    addComment: (state: any = initialStateValue,  action: Action) => {
        state.post = action.payload;
    },

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 redux 工具包初始 state 设置为 object 数组或带有 Z1E35BCBA25DBA1089C8F7BD08 的本地存储? - How to set a redux toolkit initial state to an array of object or localstorage with typescript? 如何在 redux 工具包中设置数组? - How to set array in redux toolkit? 如何在我的反应应用程序中设置 redux 商店? - How do I set up a redux store in my react application? 如何在 React 中只运行一次 redux -toolkit? - How do I run redux -toolkit only once in React? CreateEntityAdapter 中的 SetAll 不使用 react redux 工具包设置状态 - SetAll from CreateEntityAdapter do not set the state with react redux toolkit 如何取消订阅 dispatch 以在 useEffect 上加载新数据? - 我正在使用 Redux 工具包 - How do I unsubscribe to the dispatch to grab new data onload on useEffect? - I'm using Redux Toolkit 当数据是 object 而不是数组时,如何通过 Redux 工具包中的 ID 参数连接到我的 state? - How do I connect to my state by ID params in Redux Toolkit when the data is an object not an array? 使用 Redux 工具包,我如何从非反应文件访问商店? - Using Redux Toolkit, how do I access the store from a non-react file? 如何让 Redux DevTools 与 redux 工具包和 next.js(带打字稿)一起使用? - How to get Redux DevTools to work with redux toolkit and next.js (with typescript)? 如何在浏览器中为 Redux Toolkit 开启生产模式? - How do you turn on Production mode for Redux Toolkit in the browser?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM