0

我在 for 循环中的条件语句中收到错误作为意外标记。

我无法弄清楚第 46 行(即 for 循环for (var d = 1; d<=daysInWeek; d++))中的预期字符和意外字符有什么问题。

    let numberOfDays = this.state.dateClicked.daysInMonth()
    var dayArray = []
    const daysInWeek = 7;
    let weeks = parseInt(numberOfDays/7)
    for (var w = 1; w <= weeks; w++) {
      dayArray.push(<div className="row justify-content-center">)
      var dayNumber;
      for (var d = 1; d<=daysInWeek; d++) {
        dayNumber = (w-1)*(daysInWeek) + d
        dayArray.push(<div className="col-md-1" style={style.cell} key={dayNumber} onClick={this.captureDay.bind(this,{dayNumber})}>{dayNumber}</div>)
      }
      dayArray.push(</div>)
    }
    let remdays = this.state.dateClicked.daysInMonth()%7
    if (remdays>= 0) {
      dayArray.push(<div className="row justify-content-center">)
    }
    for (let r = 1; r <= remdays; r++){
      var dayN = 28+r
      <div className="col-md-1" style={style.cell} key={r} onClick={this.captureDay.bind(this,{dayN})}>{dayN}</div>
    }
    dayArray.push(</div>)
    return dayArray;
    }```

I am getting error as " Line 46:  Parsing error: Unexpected token"
(Line 46 is the for loop)
```for (var d = 1; d<=daysInWeek; d++)```
4

1 回答 1

1

这里的问题是您将一个未关闭的 div 推送到数组。您不能创建未关闭的元素并在 reactjs 中传递它们,请查看此答案以获取更多详细信息: Render unclosed html elements push into an array

对于您的问题,您可以执行类似的操作来构建您的结果。

首先,生成内部内容并将它们放入一个数组中。

 for (var d = 1; d<=daysInWeek; d++) {
    dayNumber = (w-1)*(daysInWeek) + d
    dayArray.push(<div className="col-md-1" style={style.cell} key={dayNumber} onClick={this.captureDay.bind(this,{dayNumber})}>{dayNumber}</div>)
  }

然后用父 div 包装它们,

const result = (
  <div className="row justify-content-center">
   {dayArray}
  </div>
)
于 2019-08-18T05:49:07.790 回答