It is not working can anybody help plz

const defaultState = {
login: false
};

const reducer = (state = defaultState, action) => {
// change code below this line
if (action.type === 'LOGIN') {
  state.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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: Handle an Action in the Store

Link to the challenge:

Hi, it looks like you accidentally deleted the part of the post where you explain what’s going wrong! Can you please tell us what’s happening?

@vishal34 Yes, please be very descriptive with your questions so we don’t have to go digging.

But, going off your title, and taking a quick look …

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

You’ve broken a cardinal rule of redux here - never mutate state. Every time you mutate state, Dan Abramov kills a puppy. Seriously, it is bad. So, this:

    state.login=true;

is mutating the state, it is changing the state object. In redux, you never ever change the state object. Instead, you create a new state and return that.

The instructions are:

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 .

So, the basic format of what you have is correct. The last line returns the current state, which is good - that is always your default in a reducer - just return the current state object unchanged. The issue is what is happening in the if portion. Instead of changing the current state, you should be returning what you want the new state to be. Note that there will be two return statements in your function.

If I fix that, your code passes for me.

1 Like

:scream: Nooo Dan don’t do it.

Dan is such a nice guy, I’m sure it’s a magic trick. He probably has a million puppies in his home by now, one for every time he performs his trick. :grin:

3 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.