DEV Community

Discussion on: Redux Toolkit Basic Intro

Collapse
 
markerikson profile image
Mark Erikson

Nice post, and glad to hear that RTK is working well for you!

A few quick notes on the code samples:

First, you may actually have a small issue with one of those reducers:

reducer: ( state, { payload }) => state.push(payload),

Immer wants you to either return a new value, or mutate its state, but not both. This is mutating with push(), which is fine, but also returning the result of push() because of the implicit arrow return. Immer will probably yell at you for that. You can fix it by throwing curlies around the body, or putting void in front of the body.

The findIndex() calls look like they're missing a = assignment. Personally, I'd call find() instead of findIndex():

const todo = state.find(todo => todo.id === payload.id)
if(todo) {
  todo.completed = !todo.completed;
}

Finally, while it's totally fine to use a findIndex() + a splice() to remove an item, I find it's usually shorter to do it with a .filter() instead and just return the new array same as you would with more traditional hand-written reducers. (And that's one of the nice things about Immer and RTK - you can mix and match update styles as appropriate!)

Also, the link to the "Why RTK uses thunks" post is going through an odd Youtube redirect first. The actual link is blog.isquaredsoftware.com/2020/02/... - would be good to update to point to that directly.

Collapse
 
brittneypostma profile image
Brittney Postma • Edited

Okay, I will update it now. I guess I didn't proofread very well. I believe I made all the changes except the filter instead of slice. Haven't had my coffee yet and my brain isn't working 😂

Thank you for the input and let me know if anyone else sees more typos 😉

All updated now and made sure it was functional. Helps to code with spellchecker and linter 😂