Redux: Handle an Action in the Store. I Don´t understand the challenge

In the description of the challenge it says:

  • Fill in the body of the reducer function so that if it receives an action of type 'LOGIN' it returns a state object with login set to true . Otherwise, it returns the current state*

Doubts I have about this sentence:

  1. "it returns a state object with login set to true" Not sure how to do this. So far the only method I´ve learned is store.getState, but not any like “changeState” that would be useful for this (or setState like in React)

So far I have this:

const reducer = (state = defaultState, action) => {
  if (action.type == 'LOGIN'){
    /* I know that here I have to put return state with " login: true " /* right?
  }
  else{
    return state
  }
};

link to the challenge
Thanks friends

when an reducer is called it will recieve the previous state or initial state, and an object called action. action will take on the form of something like.

const action = {
  type: 'LOGOUT'
}

an action should always contain a type. Technically type could be a property name of whatever your wanted it to be, but the general convention I’ve seen is to always use type.

Optionally a action may also contain a payload.

const action = {
  type: 'MESSAGE',
  payload: 'Hi There'
}

I hope that helps to clearify things.

  1. “Otherwise, it will” refers back to the previous sentence ‘If it receives a login action’. Otherwise (if it doesn’t receive a login action), it will …

Right so basically you’re looking to type a conditional (if statement or switch) based on action.type

The otherwise would then be the else statement or the default case

Thanks, I think I have the conditional sorted out now but I think now I´m lacking the knowledge to actually put " returns a state object with login set to true"

1 Like

Here is an example of me returning an object with name set to Ethan:

return {
  ‘name’: ‘Ethan’
}
1 Like