-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add runtime deprecation warning for object case reducer syntax in createSlice.extraReducers
and createReducer
#2301
Comments
Adding to that: the builder notation is also a lot less problematic with circular references. |
Also: it would be really neat if we could write a codemod to help with this migration! |
Hey @markerikson from Twitter! Will look in the evening what I can do in order to help to write a codemod for an easier migration I'll keep you updated! |
hey guys, I had a quick look and I was able to implement both codemods. Added also some tests. check it out. |
I agree with this idea. Personally I don't use that object syntax, I'm not sure how common pattern that is though. |
Doesn't this mean we have then again forced to manually create action creators using "createAction", instead of getting them automatically exported from slice.actions? Seems like something that is going in the wrong direction wrt some if the initial motivation behind RTK, which was to get rid of the dreaded "boilerplate". |
You are missing something. This is about Don't do this any more: extraReducers: {
[incrementBy]: (state, action) => {
return state + action.payload
},
'some/other/action': (state, action) => {},
}, Do this instead: extraReducers: (builder) => {
builder
.addCase(incrementBy, (state, action) => {
// action is inferred correctly here if using TS
})
// You can chain calls, or have separate `builder.addCase()` lines each time
.addCase(decrement, (state, action) => {})
// You can match a range of action types
.addMatcher(
isRejectedAction,
// `action` will be inferred as a RejectedAction due to isRejectedAction being defined as a type guard
(state, action) => {}
)
// and provide a default case if no other handlers matched
.addDefaultCase((state, action) => {})
}, |
Right, I came back here to write that I had now gathered that from reading the other "2.0 planning" thread :) It wasn't completely obvious that this only involved createReducer and extraReducers from reading this sentence: Anyway, I can see the motivation for doing this, no objections here :) |
createSlice.extraReducers
and createReducer
In version
You might consider to make such a way in version 2 |
@cdpark0530 : you don't. import { todoAdded } from "../someOtherFeature/someOtherFeatureSlice";
const newSlice = createSlice({
name: "whatever",
initialState,
reducers: {
// normal reducers here
}
extraReducers: (builder) => {
builder.addCase(todoAdded, (state, action) => {
// TS will know that `action` is a `PayloadAction<Todo>` because of type inference
})
}
}) |
Hey, |
@AngelHadzhiev : we changed |
We'd like to remove the object syntax for declaring case reducers in
createReducer
andcreateSlice
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 implicittoString()
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:
should be migrated to:
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
The text was updated successfully, but these errors were encountered: