i have a state in redux that it has Two dimensions 1.user id and 2.product id
return {
...state,
[action.payload.user.id]: {
...state[action.payload.user.id],
[action.payload.product.id]: {
data: action.payload.product
}
}
}
i want to keep last 10 product for each user in my state
please help me ? any suggestion?
When ever, you are adding new product to user, you get the last 9 (upto) and add the new one. Use slice(-9) will give last 9 items in array.
Update: as @JakubKotrs pointed out, Updating to maintain as array of products for user.
return {
...state,
[action.payload.user.id]: [
...state[action.payload.user.id].slice(-9),
{
[action.payload.product.id]: {
data: action.payload.product,
},
},
],
};