简体   繁体   English

Classname 道具不会随着 React 中的条件渲染而更新

[英]Classname prop won't update with conditional rendering in React

I am trying to build a carousel in react.我正在尝试在反应中建立一个轮播。 For some reason, the ${fadeIn?出于某种原因,${fadeIn? styles.fade_in: ""} line in className for rendering the animation didn't work. styles.fade_in: ""} className 中用于呈现 animation 的行无效。 Even though I set it to false when calling handelSlider, it still rendered the fade_in animation, resault the carousel automatically move to the third image即使我在调用 handelSlider 时将其设置为 false,它仍然呈现 fade_in animation,导致轮播自动移动到第三张图像

My though was using a container to wrap the picture, and put them in a parent div with flex set to 100% so I can conditianly change the position for each photo.我虽然使用容器来包裹图片,并将它们放在父 div 中,并将 flex 设置为 100%,这样我就可以有条件地更改每张照片的 position。 Each time after rendering the transition animation, I remove the animation then swap the current and next piture so it reset for the next animation. However, for some reason, even though when the fadein state set to false, the fadein css prop was still added.每次渲染过渡 animation 后,我删除 animation 然后交换当前和下一个 piture,以便为下一个 animation 重置。但是,由于某种原因,即使 fadein state 设置为 false,仍然添加了 fadein css 道具. So confused, please help `好迷茫,求助`

const Slider = () => {
  const bannerPics = [banner1, banner2, banner3];
  const [fadeIn, setFadeIn] = useState(false);
  const [prev, setPrev] = useState(0);
  const [next, setNext] = useState(1);

  const prevPic = <img src={bannerPics[prev]} alt="" />;
  const nextPic = <img src={bannerPics[next]} alt="" />;

  const handelSlider = () => {
    setFadeIn(!fadeIn);
    setFadeIn(!fadeIn);
    setTimeout(() => {
      console.log(fadeIn);
      if (next === bannerPics.length - 1) setNext(0);
      else {
        setPrev(prev + 1);
        setNext(next + 1);
      }
    }, 3000);
  };

  return (
    <div>
      <div className={styles.banner}>
        <div
          className={`${styles.pic_container} ${fadeIn ? styles.fade_in : ""}`}
        >
          {prevPic}
        </div>
        <div
          className={`${styles.pic_container} ${fadeIn ? styles.fade_in : ""}`}
        >
          {nextPic}
        </div>
      </div>
      <button onClick={handelSlider}>MOVE</button>
    </div>
  );
};

export default Slider;

` `

I think you were going for something like this:我想你正在做这样的事情:

  const handleSlider = () => {
    if (fadeIn) return;
    setFadeIn(true);
    setTimeout(() => {
      setPrev((i) => (i + 1) % bannerPics.length);
      setNext((i) => (i + 1) % bannerPics.length);
      setFadeIn(false);
    }, 1000);
  };
.pic-container {
  opacity: 1;
  transition: opacity 1s;
}

.fade-in {
  opacity: 0;
}

It's probably more accurate to call that class / variable just fade since it is controlling both fading in and out.调用 class / 变量 just fade可能更准确,因为它同时控制淡入和淡出。

https://stackblitz.com/edit/react-ts-n6sug4?file=App.tsx https://stackblitz.com/edit/react-ts-n6sug4?file=App.tsx

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

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