我是 React 和 Javascript 的新手。我有一个带有构造函数和函数的类组件,如下所示:
class MyComponent extends Component {
constructor(props) {
super(props)
this.state = {
HTMLTable : ""
}
createGrid ()
{
for(let row = 0; row <= 20;row++)
{
let currentHTMLRow = `<tr id = "Row ${row}">`;
for(let col = 0; col < 50; col++)
{
let newNodeId = `${row}_${col}`;
let NodeClass = "unvisited";
currentHTMLRow = currentHTMLRow +`<td id = "${newNodeId}" class = "${NodeClass}"></td>`
}
this.state.HTMLTable += `${currentHTMLRow}</tr>`;
}
let chart = document.getElementById("grid");
chart.innerHTML = this.state.HTMLTable;
}
}
这会产生预期的效果,但我被警告不要像这样改变状态的值
this.state.HTMLTable += `${currentHTMLRow}</tr>`;
*如何在每次循环迭代时使用 setState() 更改 HTMLTable 字符串的状态:this.state.HTMLTable += ${currentHTMLRow}</tr>; *
aluckdog
Smart猫小萌
青春有我
相关分类