Hi, I’m working on the Redux course challenge Extract State Logic to Redux and the tests keep failing on:
The store should exist and have an initial state set to an empty array.
Dispatching addMessage against the store should immutably add a new message to the array of messages held in state.
My code:
// Define ADD, addMessage(), messageReducer(), and store here:
const ADD = "ADD";
const initialState = [];
function addMessage(message) {
return ({
type: ADD,
message: message
})
}
function messageReducer(state = initialState, action) {
switch (action.type) {
case ADD: {
return [...state, action.message]
}
default:
return state;
}
}
const store = Redux.createStore(messageReducer);
console.log(store.getState())
let hi = "hi"
store.dispatch(addMessage(hi));
console.log(store.getState())
store.dispatch(addMessage("goodbye"));
console.log(store.getState())
Output:
[]
[ 'hi' ]
[ 'hi', 'goodbye' ]
I can’t see why. I’ve tried setting state = initialState
object, to []
empty array, setting state in Redux.createStore(reducer, [])
but it keeps failing on these two tests. Also I’m sure I’m creating a shallow copy of those variables when I return the new state, right?
Just to add I comment out my console.log
’s when I run the tests.