createSlice reducer types
See original GitHub issueHi,
I’ve been using a utility type like this for a while:
import { CaseReducer, PayloadAction } from '@reduxjs/toolkit'
export type MapReducerPayloads<State, ReducerPayloadMap> = {
[K in keyof ReducerPayloadMap]: CaseReducer<State, PayloadAction<ReducerPayloadMap[K]>>
}
Which allows you to succinctly create a CaseReducers
type like so:
interface RoomState {
id: string | null;
peer?: Peer;
owner?: boolean;
connections: string[];
}
type Reducers = MapReducerPayloads<RoomState, {
createRoom: void;
joinRoom: {
roomId: string;
owner: boolean;
};
}>
const initialState: RoomState = {
id: null,
connections: []
}
export const roomSlice = createSlice<RoomState, Reducers>({
name: 'room',
initialState,
reducers: {
createRoom: (state) => {},
joinRoom: (state, { payload: { roomId, owner } }) => {
state.id = roomId
state.peer = new Peer(roomId)
state.owner = owner
}
}
})
I really like this approach as you get the types flowing through into your payload objects inside your reducers functions.
Just wanted to ask:
A) Is there a similar, built-in way of accomplishing the same? (without defining the reducers outside of the createSlice
fn and letting the types get inferred - I find this less readable too)
B) Is this a pattern the maintainers would be interested in incorporating into the lib? Or is it too subjective/niche/complex etc?
Cheers
Issue Analytics
- State:
- Created a year ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
createSlice - Redux Toolkit
An object containing Redux "case reducer" functions (functions intended to handle a specific action type, equivalent to a single case ...
Read more >Understanding createSlice in Redux Toolkit — ReactJS.
It automatically generates action creators and action types that correspond to the reducers and state. In Redux-Toolkit, the createSlice method helps us ...
Read more >How To Setup Redux Slices with Redux Toolkit - SoftKraft
createSlice takes an object of reducer functions, a slice name, and an initial state value and lets us auto-generate action types and action...
Read more >Get action types from Redux Toolkit's createSlice
The following does not work, it resolves to AnyAction ... which is understandable I'd say, as all actions pass through all reducers. Dispatch< ......
Read more >Refactoring with Redux Toolkit Cheatsheet - Codecademy
name : the slice name used as the prefix of the generated action.type strings ... createSlice() returns an object containing a slice reducer...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ah, I probably should have checked that first, I humbly withdraw my suggestion 😄
Well, to be fair we do say so in the docs 😉
Here for example: https://redux-toolkit.js.org/usage/usage-with-typescript#defining-the-initial-state-type