Quiero recibir datos de createAsyncThunk de redux toolkit al enviar.
Pero no sé cómo configurar el tipo de datos.
Si no se especifica ningún tipo de datos, se muestra una línea de advertencia con una línea roja.
como esta captura de pantalla
¿Cómo especifico el tipo?
este es mi codigo
// 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); } }, );Debe declarar el tipo cuando llame a createAsyncThunk , declare una interfaz para data como este:
type DataType = { content : string postId : string }entonces añádelo aquí:
async (data : DataType, {rejectWithValue}) => {Puede leer más aquí: https://redux-toolkit.js.org/usage/usage-with-typescript#createasyncthunk
usted especifica los tipos en el archivo createSlice con interfaz o tipo
p.ej:
interface Action { type: string payload: { img: string images: string[] | [] | null } } el tipo siempre es una cadena, la carga útil es lo que pones en { } dentro del 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; },