我正在尝试使用过滤器方法从数组中删除一个项目,如下所示:
removeDisplate: (state, action: PayloadAction<string>) => {
console.log(action.payload);
state.map((item) => {
console.log(item.name);
});
state.filter((item) => item.name !== action.payload);
},
Run Code Online (Sandbox Code Playgroud)
并从我的前端调用它,如下所示:
{cart.map((displate, index) => {
return (
<Card
sx={{
minHeight: "150px",
display: "flex",
padding: "10px",
gap: "10px",
backgroundColor: "black",
margin: "10px",
position: "relative",
}}
key={index}
>
<CloseIcon
sx={{
position: "absolute",
top: "10px",
right: "10px",
color: "red",
cursor: "pointer",
}}
onClick={() => handleRemoveDisplate(displate.name)}
/>
</Card>
);
})}
Run Code Online (Sandbox Code Playgroud)
状态中的有效负载和项目名称都与控制台日志相同,但它仍然没有从数组中删除它,有什么想法吗?