Redux - Use a Switch Statement to Handle Multiple Actions

I am getting error statements that say that if I dispatched either action creator, it would not result in the desired outcome. Here are the errors in the console:

“Dispatching loginUser should update the authenticated property in the store state to true.
Dispatching logoutUser should update the authenticated property in the store state to false.”

What is the difference between the solution and my code?

  **Your code so far**
const defaultState = {
authenticated: false
};

const authReducer = (state = defaultState, action) => {
// Change code below this line
switch(action.type) {
case 'LOGOUT':
  defaultState.authenticated=false;
case 'LOGIN':
  defaultState.authenticated=true;
default:
  return defaultState;
} 
// Change code above this line
};

const store = Redux.createStore(authReducer);

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

const logoutUser = () => {
return {
  type: 'LOGOUT'
}
};
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Redux - Use a Switch Statement to Handle Multiple Actions

Link to the challenge:

The redux reducer is a pure function (function with no side effect) which should return a state, it must not mutate the state. Here, for the LOGOUT action, you modify the state. But you should return it.

So it should be :

[redacted by mod]

Like this, the reducer returns a copy of the state in which the property “authenticated” has been modified.

I let you do the same for the ‘LOGIN’ action.

Please don’t give cut and paste solutions for FCC challenges. Please provide guidance. The explanation other than that is good, though.

Well, I didn’t copy-paste the solution from the challenge, but I guess both should be closed :sweat_smile:

Sorry for that, I will provide guidance next time, I agree it is better

1 Like