简体   繁体   English

在父组件中设置 state 导致重新渲染时,React Carousel 旋转延迟问题?

[英]React Carousel rotate on delay issue when setting state in parent component causing rerender?

Hopefully, the title isn't too ambiguous, but I am struggling how to summarise my issue in just a few words.希望标题不会太模棱两可,但我正在努力如何用几句话来总结我的问题。

What I am attempting to achieve is the one image with appear for 1.5 seconds, the opacity of image will go from 0.3 to 1 and a string of text "Completed" appears below the image, and will then rotate and move to the next image where it will have the same outcome, constantly looping through each image.我试图实现的是一个出现 1.5 秒的图像,图像的不透明度将从 0.3 到 1 的 go 并且图像下方出现一串文本“已完成”,然后将旋转并移动到下一个图像它将具有相同的结果,不断循环遍历每个图像。

The issue is, it only seems to rotate once.问题是,它似乎只旋转一次。 It will update the styles and add the text below the image but fail to rotate.它将更新 styles 并在图像下方添加文本但无法旋转。

I have a 3d carousel component that rotates the items within an array logos when a function is called rotate() .我有一个 3d 轮播组件,当 function 被称为rotate()时,它会旋转数组logos中的项目。

const [rotateDeg, setRotateDeg] = useState(0);
const [currentIndex, setCurrentIndex] = useState(0);

const rotate = () => {
   const maxIndex = logos.length - 1;
   const incrementIndex = currentIndex + 1;
   const newIndex = incrementIndex > maxIndex ? 0 : incrementIndex;
   setCurrentIndex(newIndex);
   setRotateDeg(rotateDeg - 360 / logos.length);
};

return (
    <Container>
      <Carousel ref={carousel} logosLength={logos.length} rotateDeg={rotateDeg}>
        {logos.map((item, index) => {
          const { key } = item;
          return (
            <Item
              key={key}
              index={index}
              logosLength={logos.length}
              currentIndex={currentIndex}
            >
              <LoadingSpinLogo
                item={item}
                delay={1500 * (index + 1)}
                rotate={() => rotate()}
                key={key}
                isCurrent={currentIndex === index}
              />
            </Item>
          );
        })}
      </Carousel>
    </Container>
  );

The component LoadingSpinLogo is where I seem to be having an issue.组件LoadingSpinLogo似乎是我遇到问题的地方。

I have tried to pass different dependencies into the useEffect callback but it seems to cause weird issues with the delay.我试图将不同的依赖项传递给useEffect回调,但它似乎会导致延迟出现奇怪的问题。

const LoadingSpinLogo = ({ item, rotate, delay, isCurrent }) => {
  const [completed, setCompleted] = useState(false);
  const [divStyle, setDivStyle] = useState({ opacity: 0.3 });

  const props = useSpring(divStyle);

  const customStyles = {
    height: "78px",
    width: "115px",
    backgroundRepeat: "no-repeat",
    backgroundPosition: "center center",
    display: "block",
    margin: "auto"
  };

  const updateCompleted = () => {
    setCompleted(true);
    setDivStyle({ opacity: 1, from: { opacity: 0.3 } });
    rotate();
  };

  useEffect(() => {
    const timeoutID = setTimeout(function() {
      updateCompleted();
    }, delay);
    return () => {
      // Clean up the subscription
      window.clearInterval(timeoutID);
    };
  }, []);

  return (
    <LoadingSpinContainer>
      <animated.div style={props}>
        <ImageServer png={item.url} customStyles={customStyles} />
      </animated.div>
      {completed ? "Completed" : null}
    </LoadingSpinContainer>
  );
};

Here is a CodeSanbox of my components.这是我的组件的CodeSanbox

Any idea where I am going wrong here?知道我在哪里出错了吗?

Any help would be greatly appreciated.任何帮助将不胜感激。

Removing the [] in your useEffect function seems to works fine for me删除useEffect function 中的[]似乎对我来说很好用


  useEffect(() => {
    const timeoutID = setTimeout(function() {
      updateCompleted();
    }, delay);
    return () => {
      // Clean up the subscription
      window.clearInterval(timeoutID);
    };
  }, );

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

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