简体   繁体   English

如何使用 ReactJs 切换元素的 class

[英]How to toggle class of an element with ReactJs

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).当单击列表中的一个图像时(但仅在单击的图像中),我需要切换一些 css 效果。 I did this but is not working.我这样做了,但没有工作。

I'm trying to do it using "classList.toggle('selected')", but the css doesn't change.我正在尝试使用“classList.toggle('selected')”来做到这一点,但 css 没有改变。 I don't know if there is some way to do it with 'useState'.我不知道是否有某种方法可以使用“useState”来做到这一点。

I'm learning reactjs, i hope someone can help.我正在学习reactjs,希望有人能帮忙。

REACTJS 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 SCSS

img {
  height: 100%;
  width: 100%;
  cursor: pointer;
  object-fit: cover;

  &:hover {
    filter: blur(1px);
  }

  &.selected {
    border: 1px solid black;
  }

}      

You should absolutely use and change state to cause changes to the DOM.您绝对应该使用和更改 state 以导致对 DOM 的更改。 In React, never use native DOM methods like classList.toggle .在 React 中,永远不要使用像classList.toggle这样的原生 DOM 方法。

For this case, use a numeric index state of the index of the selected image.对于这种情况,请使用所选图像索引的数字索引 state。 First have an array of srcs, eg首先有一个src数组,例如

const srcs = [Surf, Barco, ...]

Then map over them, with a click handler that uses the iteration index to decide how to change the selected index in state:然后 map 覆盖它们,点击处理程序使用迭代索引来决定如何更改 state 中的选定索引:

const [indexSelected, setIndexSelected] = useState(-1);
const handleClick = i => () => {
  setIndexSelected(i === indexSelected ? -1 : indexSelected);
};
// ...
  <div className={styles.photoArea}>
    <div>
      {
        srcs.map((src, i) => <img
          onClick={handleClick(i)}
          className={i === indexSelected ? 'selected' : ''}
          src={src}
        />)
      }
    </div>

I made it work by doing some changes.我通过做一些改变使它工作。

REACT反应

const Photos = () =>{

  const[mainPhoto, setMainPhoto] = useState(Barco)
  const[selected, setSelected] = useState(0)

  const changePhoto = (element, index) => {
    setMainPhoto(element.target.src)
    setSelected(index)
  }

  const images = [Barco, Surf]

  return(
      <section className={styles.sectionContainer}>
        <div className={styles.imgContainer}>
          <img src={mainPhoto}/>
          <button className={styles.previous} >
            <GrPrevious size={24} />
          </button>
          <button className={styles.next}>
            <GrNext size={24}/>
          </button>
        </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>Guilherme Donada</li>
              <li>Guilherme Do nada</li>
            </ul>
          </div>
          <div className={styles.photoArea}>
            <div>
            {images.map((image,index) => (              
              <img 
                key={index}
                onClick={ (e) => changePhoto(e, index)}  
                src={image} 
                className={ index == selected ? styles.selected : '' }
              />
            ))}
            </div>
          </div>
        </div>
      </section>
  )
}

export default Photos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM