question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

createSlice reducer types

See original GitHub issue

Hi,

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:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ceukcommented, Apr 28, 2022

Ah, I probably should have checked that first, I humbly withdraw my suggestion 😄

0reactions
phryneascommented, Apr 26, 2022

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 image

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found