Mh, seems you’re trying to use much more stuff than needed ^^
In your initialstate definition you’re not asked to create an object with a key:value pair named arr:[], the challenge asks for an initialstate equal to an empty array. As simple as initialstate= []^^
This is not an error, but why to have your variable nested into an object when you can use it as is
(in the reducer obviously you will replace initialstate.arr with initialstate if you follow along)
The error is in the first return statement of the reducer: you should return the old messages with the new one added.
Here a link for the spread operator ( MDN - Spread operator ) : you can use it simply ‘spreading’ the old values and adding the new one ( almost as you did into the arr definition of that statement.
The spread operator is not mandatory though: you can use concat array method ( MDN - Array.prototype.concat ) like oldarray.concat(newValueToAddToTheOldArray) and everything will be fine aswell ^^
I came across this thread because I also had issues passing this challenge with a variety of different ways of copying the state and adding the action.message to it
(eg. I tried the above spread operator method, I tried slice and assign to a new variable and then push the msg onto that new variable, I tried concat, etc). None worked.
The only way that worked for me was a switch statement.
In the following challenge (which is a continuation one), the code provided starts out with a switch as well, so perhaps this is why nothing else I tried got past the challenge test suite.
I’m just writing this post now to help anyone who is looking on the forum having tried obvious solutions that failed. Try a switch statement…
I just copy pasted the above code (corrected) and it gives no problem^^
What browser you using? The OP is on firefox, I’m on firefox ESR, chances you’re using chrome?
Here is the code I tried:
// define ADD, addMessage(), messageReducer(), and store here:
const initialstate = [];
const ADD = 'ADD';
function addMessage(message){
return {type:ADD,
message: message};
};
const messageReducer=(state=initialstate, action)=>{
if (action.type===ADD){
return [...state, action.message]
}else{
return state;
}
}
const store =Redux.createStore(messageReducer);