I have a List of Objects looking like this:
const List = [
{
title: " ",
timestamp: 500,
seen: false
},
{
title: " ",
timestamp: 600,
seen: false
},
The List ist displayed as a List auf Buttons with a title an an icon. When a Button is clicked, the object should be marked as "seen: true" and the icon should change the color. My problem: In my chase, the icon only changes the color when the component is rerendert, it should change when the button is clicked though. Can someone help?
const styles: Styles = {
icon_unseen: {
color: "#f97671",
},
icon_seen: {
color: "#bdbdbd",
}
};
export const = () => {
const sortedList = List.sort((a, b) => a.timestamp - b.timestamp);
const onClick = (event: any) => {
List.map((item) =>item.timestamp==event.currentTarget.id? item.seen=!item.seen : "")
};
return (
<Box>
<PopperUnstyled>
<StyledPopperDiv>
{sortedNList.map((element) => (
<ListItemButton id={element.timestamp}
onClick={onClick}
sx={{borderRadius: 2}}>
<ListItem>
<ListItemText
primary={<Typography>{element.title}</Typography>
/>
<FiberManualRecordRoundedIcon sx={element.seen ? styles.icon_seen : styles.icon_unseen}/>
</ListItem>
</ListItemButton>
<ListItemText
primary={<Typography>{element.title}</Typography>
/>
<FiberManualRecordRoundedIcon sx={element.seen ? styles.icon_seen : styles.icon_unseen}/>
))}
</StyledPopperDiv>
</PopperUnstyled>
</Box>
);
};