Hello campers,
I need help with this challenge: Handle an Action in the Store
My solution to this challenge is:
let localState = state;
if(action.type==='LOGIN'){
localState.login = true;
}
return localState;
But it doesn’t complete the challenge, so I went to the Hint and the solution seems to be the same as mine:
if (action.type === "LOGIN") {
return {
login: true
};
} else {
return state;
}
What am I doing wrong?
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>
) will also add backticks around text.
Note: Backticks are not single quotes.

1 Like
You’re mutating the original value of the state, what you’ve done is not the same as the hint solution, which returns a completely new state.
This
let localState = state;
Is not a copy, it’s the same object. JavaScript doesn’t work like that.
1 Like
Thanks! Solve it. I did:
let localState = {...state};
1 Like
This solves it, but it’s not quite what you want in reality: if you look at the what the hint solution is doing
- if something changes, rebuild the state as a new value
- if nothing changes just return the state (nothing changes)
So if you want to do what you’re doing (which is fine atm), you would do
let localState = {...state};
if(action.type==='LOGIN'){
localState.login = true;
}
return state;
Note that this is not a terrific pattern because you’ll end up a. writing more code, and b. it explicitly mutates the state locally, and it’ll start to get hard to track in your head as the state gets more complex. It’s generally simpler to just return completely new values in the branch of the conditional. As I say though, it’s fine. Just keep this point in mind tho and think about adjusting that style when you start to get anything confusing.