I wan’t to make the only selected button change not all in the same time when i clicke on it
I’ve created this function which contains a Boolean state and the toggle it
const [like, setLike] = useState(false);
const handleLike=()=>{
setLike(!like)
console.log(like);
}
and called it here iside the map
return (
<Grid container spacing={1} px="4%" width='100%' >
{CardData.map((e, idx) => (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "center",
padding: 0,
border: "1px solid #e0e0e07a",
position: "relative",
borderRadius: "1.5rem",
width: "94%",
boxShadow: "5px 5px 46px -46px #000000",
}}
mb={5}
key={idx}
>
<Box width='100%' >
<Box position="absolute" top=".4rem" right=".8rem">
<IconButton
aria-label="fingerprint"
color="default"
sx={{
zIndex: "4",
bgcolor: "#4b4d4eb2",
width: "2rem",
padding: "4px",
}}
onClick={(e)=>handleLike(e)}
>
{like?<FavoriteIcon sx={{ width: ".8em", color: "#fff" }} />: <FavoriteBorderIcon sx={{ width: ".8em", color: "#fff" }} />}
</IconButton>
</Box>
</Box>
</Box>
</Grid>
2
Answers
You should create a like useState for each Box. You can do something like this:
First, make your
like
state into an array of likes:Then, make sure to operate on the correct item in the array:
Importantly, this will only work if elements are in consistent positions in the array. If they could get moved around, then use some other unique key to identify the correct item to like.