Redux default state/current state

how is this default state the current state ?
are we going to change the value in defaultstate during runtime? but it’s declared const to be immutable, whilst being reset? am i getting this?


const defaultState = {
authenticated: false
};

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

  case "LOGOUT":
    return {
      authenticated: false
    };

  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/96.0.4664.45 Safari/537.36

Challenge: Use a Switch Statement to Handle Multiple Actions

Link to the challenge:

Default arguments say “if the value is undefined, use the default provided”. It’s only undefined the first time it runs.

Edit:

but it’s declared const to be immutable, whilst being reset?

No, const means you can’t reassign the variable called defaultState in the same scope

const defaultState = {};
defaultState = "I will cause an error";

Nothing is being reassigned here, it’s just saying “use this object if nothing is available”, which will occur exactly once.

ah, so that defaultState object is just not in use in another state?
then i guess i am confused by the given func:
authReducer = (state = defaultState...

The reducer function has to start with something when it runs, you’ve defined what the state should be initially, where else would it get it from?

1 Like