i need to toggle some css effect when one of the images of the list is clicked (but only in the image that was clicked). I did this but is not working.
I'm trying to do it using "classList.toggle('selected')", but the css doesn't change. I don't know if there is some way to do it with 'useState'.
I'm learning reactjs, i hope someone can help.
REACTJS
const Photos = () =>{
const[mainPhoto, setMainPhoto] = useState(Barco)
const changePhoto = (element) => {
setMainPhoto(element.target.src)
let target = element.currentTarget;
target.classList.toggle('selected')
}
return(
<section className={styles.sectionContainer}>
<div className={styles.imgContainer}>
<img src={mainPhoto}/>
</div>
<div className={styles.titleContainer}>
<div className={styles.infoArea}>
<h1>LOren </h1>
<p className={styles.resume}>LOren</p>
<p className={styles.about}>LOren LOren LOren LOrenLOrenLOrenLOren LOrenLOren LOren LOren LOren LOrenLOren
LOren LOrens
</p>
<h2>Fotografia:</h2>
<ul>
<li>User 01</li>
<li>User 02</li>
</ul>
</div>
<div className={styles.photoArea}>
<div>
<img onClick={ (e) => changePhoto(e)} src={Surf}/>
<img onClick={ (e) => changePhoto(e)} src={Barco}/>
<img onClick={ (e) => changePhoto(e)} src={Barco}/>
<img onClick={ (e) => changePhoto(e)} src={Barco}/>
<img onClick={ (e) => changePhoto(e)} src={Barco}/>
<img onClick={ (e) => changePhoto(e)} src={Barco}/>
<img onClick={ (e) => changePhoto(e)} src={Barco}/>
<img onClick={ (e) => changePhoto(e)} src={Barco}/>
<img onClick={ (e) => changePhoto(e)} src={Barco}/>
<img onClick={ (e) => changePhoto(e)} src={Barco}/>
<img onClick={ (e) => changePhoto(e)} src={Barco}/>
</div>
</div>
</div>
</section>
)
}
export default Photos
SCSS
img {
height: 100%;
width: 100%;
cursor: pointer;
object-fit: cover;
&:hover {
filter: blur(1px);
}
&.selected {
border: 1px solid black;
}
}