If you don't want to add this information to the main state (posts
) you can create a separate list of selected ids, it'll be something like this (assuming you're using function components)
const [selectedPosts, setSelectedPosts]=useState([]);
...
{posts.map((post)=> {
return (
<div className={selectedPosts.includes(post.id) && "myClass"} key={post.id}>
{post.title}
<button onClick={()=> {setSelectedPosts(s => [...s, post.id])}}>add</button>
</div>
)
})}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…