THis is my code:
import { Conversation } from "@/types/conversation";
import { PayloadAction, createSlice } from "@reduxjs/toolkit";
const initialState: Conversation | null = null;
export const conversationSlice = createSlice({
name: "conversation",
initialState,
reducers: {
setConversation: ( // type error
state: Conversation | null,
action: PayloadAction<Conversation>
) => {
state = action.payload;
return state;
},
},
});
// Action creators are generated for each case reducer function
export const { setConversation } = conversationSlice.actions;
export default conversationSlice.reducer;
THis is the error I am receiving:
Type '(state: Conversation | null, action: PayloadAction<Conversation>) => Conversation' is not assignable to type 'CaseReducer<null, { payload: any; type: string; }> | CaseReducerWithPrepare<null, PayloadAction<any, string, any, any>>'.
Type '(state: Conversation | null, action: PayloadAction<Conversation>) => Conversation' is not assignable to type 'CaseReducer<null, { payload: any; type: string; }>'.
Type 'Conversation' is not assignable to type 'void'.ts(2322)
I know that immer is used under the hood of createSlice
. WHen I remove return statement from setCOnversation
- error is gone but reducer does not work. If I leave it like that, reducer works (organization sets) but I have an error.
How to fix it ?
I have tried to get rid of return statement, but it brokes application in the other way. ALso read redux toolkit docs
Still getting an error:
import { Draft, PayloadAction, createSlice } from "@reduxjs/toolkit";
interface Conversation {
any_data: string[];
}
const initialState: Conversation | null = null;
export const conversationSlice = createSlice({
name: "conversation",
initialState,
reducers: {
setConversation: (
// type error
state: Draft<Conversation | null>,
action: PayloadAction<Conversation>
) => action.payload,
},
});
// Action creators are generated for each case reducer function
export const { setConversation } = conversationSlice.actions;
export default conversationSlice.reducer;
Via Active questions tagged javascript - Stack Overflow https://ift.tt/HJd3Ith
Comments
Post a Comment