Hi, can someone please explain what action.payload is in simple English please…
thanks in advance
2 days no reply… action.payload what are you exactly ? Maybe this will get me a reply…
state.cartItems.filter(item => item.id !== action.payload). I want to understand how the statement ( item.id !== action.payload ) removes the item from the cart.
action.payload… action.payload… action.payload… what are you exactly ?
Hey, friend.
Have you any ideas about action in Redux at first?
If yes or not, I will change my step for explanation.
Hi… yes I do, but now I’m using Redux toolkit. I see a lot of examples with this action.payload, but what I don’t understand is how is [ state.cartItems.filter(item => item.id !== action.payload) ] removing an item from the cart. I want to understand the statement (item.id !== action.payload).
thank you in advance
In Redux Toolkit, the action.payload
often contains the specific data you want to act upon in your reducer. In this example, action.payload
likely holds the id
of the item you want to remove from the cart.
state.cartItems.filter(item => item.id !== action.payload)
Thank you Marc3 and thank you for responding. But still it doesn’t answer the fact that I don’t understand the statement ( item.id !== action.payload ). Ok , to me (item.id) means the item I’m working with or I could say this.item, the current item. Now after the arrow => the item.id !== action.payload, I understand that you want to return something from your function. Now how is this.item or item.id not being explicitly equal to action.payload(!== action.payload) removing the item from the cart. I haven’t stated how I’m removing the item from the cart.
I’m still struggling with this statement ( item.id !== action.payload ). I have read somewhere where they use an if statement:
if (todo.id !== action.payload) {
}
They say that: If (this isn’t the todo item we’re looking for)
I am using this ( item.id !== action.payload ) that I’m not understanding… but I’m doing my learning no good.
Finally Marc3 I found a solution that answers my question. I came across this filter explanation code:
let numbers = [1, 2, 3, 4, 5];
numbers = numbers.filter(number => number !== 3);
console.log(numbers); // Outputs: [1, 2, 4, 5]
I’m so happy… from this code I understand that 3 is the number I want to remove from the array, so I just replaced 3 with action.payload.
So to me action.payload is the dynamic way to remove an item, or element by it’s id from an array. Thank God and thank you for your help