I have stumbled upon a problem when adding an element inside an object inside an array. What I would like to do is add the element isFavorite: true. I am currently attempting to do so by doing the folowing:
{
...state,
list: state.list.map(item =>
item.id === action.payload.libraryContentId ?
{ ...item, isFavorite: true }
: item)
}
The payload contains the following object:
{
id: 1,
name: 'Name',
libraryContentId: 3
}
But the above does not seem to be updating the item and add the element. What do you guys suggest?
Edit: More examples of the code.
Action:
try {
const response = await axios.post(URL, {params});
dispatch({ type: ADD_FAVORITE, payload: response.data });
catch (error) {
dispatch({ type: ERROR });
}
Response Data:
{
id: 117
libraryConntentId: 245
}
List item sample:
{
id: 245,
name: 'Name',
isFavorite: false
}
Thank you!
()around your overwriting statement? Like so({ ...item, isFavorite: true })item.id === action.payload.libraryContentIddoes return the correct item when I log it to the console. Is there anything else that you see?