What is wrong with my reducer function?

Tell us what’s happening:
The reducer function seems correct as the challenge wants. What I am missing here?

Your code so far


const defaultState = {
    login: false
};

const loginAction = () => {
    return {
          type: 'LOGIN'
    }
};

const reducer = (state = defaultState, action) => {
    // Change code below this line
    let localState = state
    if (action.type === 'LOGIN') {
        localState.login = true
    }
    return localState
    // Change code above this line
};

const store = Redux.createStore(reducer);


Challenge: Handle an Action in the Store

Link to the challenge:

In the challenge description, you see the following:

Another key principle in Redux is that state is read-only. In other words, the reducer function must always return a new copy of state and never modify state directly.

You are currently modifying state directly. Why? Because the following creates a variable named localState that references the state variable. This line does not create a copy of state, therefore any changes to localState makes changes to state.

1 Like

Now it makes sense.
localState was just reference to the actual state, not a copy of it.

Thanks.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.