The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Redux: Writing a Todo List Reducer (Toggling a Todo)

Redux: Writing a Todo List Reducer (Toggling a Todo)

2:47
Learn how to implement toggling a todo in a todo list application reducer.
Watch this lesson now
Avatar
egghead.io

Learn how to implement toggling a todo in a todo list application reducer.

Avatar
Omri Mor

Thanks for the videos Dan! Very informative and well structured. I try to write the tests first on my own before I watch you do it. Was surprised to see I came up with a solution different than yours. Want to post here for review to see if my solution misses edge cases or has performance issues. Relevant code below :

const todoReducer = (state = [], action) => {
    switch (action.type) {
        case "TOGGLE_TODO":
            return [
                ...state.slice(0, action.id),
                {...state[action.id], isCompleted: !state[action.id].isCompleted},
                ...state.slice(action.id + 1)
            ]
        default:
            return state;
    }
}

thanks!

Avatar
dan entous

change the ids on the todos so they don’t follow a sequential pattern or match their position in the array.

In reply to Omri Mor
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?