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.

Add runtime deprecation warning for object case reducer syntax in `createSlice.extraReducers` and `createReducer`

See original GitHub issue

We’d like to remove the object syntax for declaring case reducers in createReducer and createSlice in RTK 2.0. The object form was a neat trick when RTK was getting started, and admittedly played nice with action creators having an implicit toString() that returned the action type. But, the builder syntax is basically the same number of lines of code, and also provides much better type safety with TS.

In other words, this:

const todoAdded = createAction('todos/todoAdded');

createReducer(initialState, {
  [todoAdded]: (state, action) => {}
})

createSlice({
  name,
  initialState,
  reducers: {},
  extraReducers: {
    [todoAdded]: (state, action) => {}
  }
})

should be migrated to:

createReducer(initialState, builder => {
  builder.addCase(todoAdded, (state, action) => {})
})

createSlice({
  name,
  initialState,
  reducers: {},
  extraReducers: builder => {
    builder.addCase(todoAdded, (state, action) => {})
  }
})

We should set up a one-warning-per-app load deprecation notice, the way React does:

https://github.com/facebook/react/blob/d5f1b067c8bbb826b823d0354a28ba31078b70c0/packages/react/src/ReactContext.js#L44-L65

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:5
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
martinerkocommented, May 3, 2022

hey guys, I had a quick look and I was able to implement both codemods. Added also some tests. check it out. In next PR I can add some codemod runner and README.

3reactions
phryneascommented, May 3, 2022

Adding to that: the builder notation is also a lot less problematic with circular references.

Read more comments on GitHub >

github_iconTop Results From Across the Web

createSlice - Redux Toolkit
A function that accepts an initial state, an object of reducer functions, and a "slice name", and automatically generates action creators and ...
Read more >
Trying to pass action.payload from one slice to another ...
Just add an extraReducer for getLoginUser.rejected to the second slice ... The object notation you are using is soon going to be deprecated...
Read more >
redux-toolkit.umd.js.map - UNPKG
Did you pass an object from inside an immer function to an async process? ... A mapping from action types to case reducers...
Read more >
Modern Redux with Redux Toolkit, June 2022 - Mark's Dev Blog
Redux Toolkit: createReducer() ... with auto-generated actions }, extraReducers(builder) { // Add reducers for additional ... Visual Deprecation Warning.
Read more >
@reduxjs/toolkit: Versions | Openbase
Deprecations and Removals Object Argument for createReducer and createSlice. ... Add runtime deprecation warning for reducer object notation by @markerikson ...
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