This code doesn’t pass the last test which is “Dispatching an action of type ADD_TO_DO on the Redux store should add a todo item and should NOT mutate state.”?
const ADD_TO_DO = 'ADD_TO_DO';
// A list of strings representing tasks to do:
const todos = [
'Go to the store',
'Clean the house',
'Cook dinner',
'Learn to code',
];
const immutableReducer = (state = todos, action) => {
switch(action.type) {
case ADD_TO_DO:
// don't mutate state here or the tests will fail
let a = state.map((item)=>item);
a.push(action.todo);
return a;
default:
return state;
}
};
// an example todo argument would be 'Learn React',
const addToDo = (todo) => {
return {
type: ADD_TO_DO,
todo:'Learn React'
}
}
const store = Redux.createStore(immutableReducer);
This is the code that got me passing the exercise:
const immutableReducer = (state = todos, action) => {
switch(action.type) {
case ADD_TO_DO:
// don't mutate state here or the tests will fail
return state.concat(action.todo);
default:
return state;
}
};
Hi, I have the same problem, the code it’s seem OK, don’t mutate state and return a new array with the new todo item but doesn’t pass the last test.
const ADD_TO_DO = 'ADD_TO_DO';
// A list of strings representing tasks to do:
const todos = [
'Go to the store',
'Clean the house',
'Cook dinner',
'Learn to code',
];
const immutableReducer = (state = todos, action) => {
switch(action.type) {
case ADD_TO_DO:
// don't mutate state here or the tests will fail
let a = state.concat(action.todo)
console.log(state) // ["Go to the store", "Clean the house", "Cook dinner", "Learn to code"]
console.log(a) // ["Go to the store", "Clean the house", "Cook dinner", "Learn to code", "Learn React"]
return a;
default:
return state;
}
};
// an example todo argument would be 'Learn React',
const addToDo = (todo) => {
return {
type: ADD_TO_DO,
todo: 'Learn React'
}
}
const store = Redux.createStore(immutableReducer);
@SoulTrain just a question regarding your solution. Not criticizing or anything, it worked for me as well, but I am just trying to understand why. Doesn’t the concat method add to what’s already initialized, i.e ‘todos’. That way it will be returning an array with the new elements on top of the existing ones and not a new array with the new elements alone…
the Array.map and Array.slice functions are also non-mutating. However, I could’t get those to work for some reason.
Please if anyone can answer me it would be awesome. Really need to grasp this.
hi, i noticed we can’t use .push? why is that?
i managed to pass my code with
let newArr =[…state];
return newArr.concat(action.todo)
but when i use .push(action.todo ) it says ‘Dispatching an action of type ADD_TO_DO on the Redux store should add a todo item and should NOT mutate state’. i don’t get how it’s mutating the state when i defined a let newArr
inside and used the spread operator to store the state. can someone explain?
Hi, thanks for the reply,
I was using the same method I used to pass the challenge and I defined the new array inside the case of reducer, just before the return statement like below
[spoiler]```
const ADD_TO_DO = ‘ADD_TO_DO’;
// A list of strings representing tasks to do:
const todos = [
‘Go to the store’,
‘Clean the house’,
‘Cook dinner’,
‘Learn to code’,
];
const immutableReducer = (state = todos, action) => {
switch(action.type) {
case ADD_TO_DO:
// don’t mutate state here or the tests will fail
let newArray = […state];
return newArray.push(action.todo) // " This is the line where i used concat instead of push to pass the code"
default:
return state;
}
};
// an example todo argument would be ‘Learn React’,
const addToDo = (todo) => {
return {
type: ADD_TO_DO,
todo
}
}
const store = Redux.createStore(immutableReducer);
I was tearing my hair out for over an hour on this one… the solution was obvious, but what I did was to test my solution by adding store.dispatch(addToDo(‘Get something to eat’)); to test what I was doing. Seemed legit. But when I tested, it kept failing…
Bottom line - there’s a problem (imho) with testing if you can’t actually run it the way you’d think you should be able to. Unless I’m missing something here??? Like I said, take the last line out and the test passes just fine…
Perhaps we aren’t covering this yet (probably in React & Redux is my guess), but where would someone actually write or put what ‘todo’ they wanted to add to their list? Are we just to assume that is this particular exercise, we’re adding a blank?