color => color.id !== id
is a predicate that's used to remove the color of the id that is passed to your removeColor
function.
How this works is the filter function will iterate through each item of the array (colors in this example) and pass that item to a function you provide to check if it should be removed from the list. If the function is true, it's removed.
color => color.id !== id
is the function that's called for each item, so if the current color's id equals the id that is passed to the removeColor
function, then it's removed.
One thing to note is the original array isn't changed, it just returns a new array (newColors
) with the items removed.
// function that takes an id as a parameter
const removeColor = id => {
// remove the color of the id that is passed by using the filter function
const newColors = colors.filter(color => color.id !== id);
// update your state with filtered colors
setColors(newColors);
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…