For useCallback, would we benefit from passing the product.id into the function? instead of accessing the variable from outside the function
Not really. You could do that:
const removeFromCart = (id) => dispatch(removeItem(id)); return ( <Button onClick={() => removeFromCart(product.id)}>Delete</Button> );
but then we might as well go all the way
return ( <Button onClick={() => dispatch(removeItem(product.id))}>Delete</Button> );
And then it's apparent we're at the same position of accessing the ID from outside the function call. I like to define them outside of the JSX, and then when we add the useCallback function it stays nice and clean and separated
useCallback
But it's pure
PURE
return ( <Button productId={product.id} onClick={e => ( dispatch(removeItem(e.target.getAttribute("productId"))) )} >Delete</Button> );
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
For useCallback, would we benefit from passing the product.id into the function? instead of accessing the variable from outside the function
Not really. You could do that:
but then we might as well go all the way
And then it's apparent we're at the same position of accessing the ID from outside the function call. I like to define them outside of the JSX, and then when we add the
useCallback
function it stays nice and clean and separatedBut it's pure
PURE