I have store with arrays (store.calendar[0].todos[{title: title, etc...}])
0: {todos: Array(0)}
1: {todos: Array(0)}
2: {todos: Array(0)}
3: {todos: Array(0)}
4: {todos: Array(0)}
I need to add action object to the index todos of array: I have tried with this reducer, but I get an error:
state.calendar[newTodo.day].concat is not a function
My reducer:
let initialState = { calendar: []}
for (let i = 1; i <= 30; i++) {
initialState.calendar.push({ todos: []});
}
const todosReducer = (state = initialState, action) => {
switch (action.type) {
case ADD_TODO:
const newTodo = action.todoItem;
const newStore = {...state,
todos: state.calendar[newTodo.day].concat(newTodo)};
return newStore;
default:
return state
}
}
export default todosReducer;
My action:
export const addTodoAction = (todoItem) => {
return {
type: ADD_TODO,
todoItem
}
}
My add todo function:
const handleSaveTodo = () => {
props.addTodo({
day: 5,
title: trackInput.value,
description: trackTextarea.value,
completed: false
});
}