Reputation: 23
We have some todo list. We have initialState kind of of like...
initialState = [ {
id: 0,
title: "first",
items: [
{
title: "one from first",
id: 0,
cards: [
{
id: 0,
text: "111",
},
{
id: 1,
text: "222",
},
],
},
{
title: "two from first",
id: 1,
cards: [
{
id: 0,
text: "www",
},
{
id: 1,
text: "zzz",
},
],
},
], }, {
id: 1,
title: "second board",
items: [
{
title: "first from second",
id: 0,
cards: [
{
id: 0,
text: "asd",
},
{
id: 1,
text: "fasdf",
},
],
},
], }, ];
We wont normalize this state for some reasons. I have to add new List. The reducer here:
switch(action) {
case CONSTANTS.ADD_LIST:
const newList = {
title: action.payload.title,
cards: [],
id: listID,
};
listID += 1;
return {
...state,
[action.payload.boardID]: {
...state[action.payload.boardID],
items: [...state[action.payload.boardID].items, newList],
},
};
}
Example. I want to add newList for items from board 1(starting from 0).
I tested another examples like the above and it's useless.
So I can't get why it's not working. I think I have some problems with docs redux and I can't get it.
Thanks!
Upvotes: 2
Views: 49
Reputation: 1548
Your initialState seems to be an Array and not an object:
initialState = [ {Board1},{Board2} ]
But your reducer seems to be returning an object, so perhaps thats a potential issue.
======
You could instead try the following changes in your reducer:
items
prop, and create its shallow copycards
prop and create its copy// Pseudo code inside reducer
const newBoardsArr = state.map(board => {
return {
...board,
items: board.items.map(item => ({
...item,
cards: item.cards.map(card => ({
...card
}))
}))
}
});
// lets say we want to update first board's items, then:
newBoardsArr[action.payload.boardID].items = newList;
return newBoardsArr;
Upvotes: 1