Handle an Action in the Store ternary operator does not work

Handle an Action in the Store ternary operator does not work
0.0 0

#1

Tell us what’s happening:

Why doesn’t a ternary operator work in this case? The if statement works with no issue, but if I replace

 if (action.type === 'LOGIN') {
    return {
      login: true
    }
  }

  return state;

with

action.type === 'LOGIN'? {
 login: true
}: state

it tells me “Cannot read property ‘login’ of undefined”

Your code so far


const defaultState = {
  login: false
};

const reducer = (state = defaultState, action) => {
  // change code below this line
  if (action.type === 'LOGIN') {
    return {
      login: true
    }
  }

  return state;
  // change code above this line
};

const store = Redux.createStore(reducer);

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/redux/handle-an-action-in-the-store/


#2

Where is your return statement?


#3

According to the ternary statement documentation: If condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2.

condition ? expr1 : expr2

The values are automatically returned per ternary documentation.


#4

That is true, but you must still use the return keyword in front of the expression.


#5

Oh I see. Thanks! i.e. return the value returned by the following expression