Add runtime deprecation warning for object case reducer syntax in `createSlice.extraReducers` and `createReducer`
See original GitHub issueWe’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:
Issue Analytics
- State:
- Created a year ago
- Reactions:5
- Comments:11 (4 by maintainers)
Top 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 >
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
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.
Adding to that: the builder notation is also a lot less problematic with circular references.