【问题标题】:Undefined element of style in reactjsreactjs中未定义的样式元素
【发布时间】:2020-07-15 12:01:50
【问题描述】:

我正在构建一个小型排序可视化器,即使定义了每个样式属性,我也会收到此错误。 我的 app.js 代码在这里 错误显示在数字 1 和 2 处(我在代码的 cmets 中提到了 1 和 2。

mergeSort(){
     // mergeSort animation is a different function
     const animations=mergeSortAnimations(this.state.array);
     for(let i=0;i<animations.length;i++){
        //take all the array bars
     const arrayBars=document.getElementsByClassName("array-bar");
     const isColorChange = i % 3 !== 2;
     if(isColorChange){
       const [barOneIdx,barTwoIdx]=animations[i];
       const barOneStyle=arrayBars[barOneIdx].style; // 1
      
       const barTwoStyle =arrayBars[barTwoIdx].style; // 2
       
       const colour = (i%3 === 0)?FIRST_COLOR:SECOND_COLOR ;
       setTimeout(() => {
           barOneStyle.backgroundColor=colour;
           barTwoStyle.backgroundColor=colour;
       },i*3); 
     }else{
         setTimeout(() => {
         const [barOneIdx,newHeight]=animations[i];
          const barOneStyle=arrayBars[barOneIdx].style;
          barOneStyle.height=`${newHeight} px`;
         },i*5);
     }
       
    }
    };

并且排序算法是正确的,我是反应 js 的新手。非常感谢任何帮助。

【问题讨论】:

  • 您可以考虑删除所有与问题无关的代码吗?
  • 对不起..我编辑了这个问题。
  • 当我评论 barTwoStyle 元素时,它没有显示任何错误。但我需要该语句来显示数组中的排序方式

标签: javascript css reactjs compiler-errors styles


【解决方案1】:

我发现了我的代码中的错误。虽然动画数组在另一个函数中得到更新,但它正在将单个值而不是 2 个值推送到该对中。因此它无法确定哪个部分该栏必须设置样式。

【讨论】:

    猜你喜欢
    • 2017-02-26
    • 2019-05-08
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    相关资源
    最近更新 更多