首页 > 解决方案 > 使用 React 在循环中更改路径

问题描述

我想用循环改变javascript中的样式。在 Vanilla js 中,这有效:

for(let i=1; i<13; i++){
const path1 = document.querySelector(".path"+i);
 path1.style.animation = "animate 1.4s forwards";
}

但现在我尝试学习 React,但我很难达到同样的效果。我使用参考:

startpath1 = createRef();
for(let i=1; i<13; i++){
    this.startpath1.current.style.animation = "animate 1.4s forwards";
}

此代码有效,但我如何在此处更改“startpath1”,例如 startpath + i

我将不胜感激您的帮助!

标签: javascriptreactjsloopsfor-loopstyles

解决方案


在数组中插入 startpath 就足够了

const tab = [this.startpath0, this.startpath1]
for(let i=0; i<13; i++){
            tab[i].current.style.animation = "animate 1.4s forwards";
            await sleep(200);
        }

推荐阅读