我有这个反应组件。这没有正确渲染,但会收到一个烦人的警告,例如
函数作为 React 子级无效。如果您返回 Component 而不是从渲染中返回,则可能会发生这种情况。或者,也许您打算调用此函数而不是返回它。
这是我的组件。我在这里做错了什么?
import React, { Component } from 'react';
class Squares extends Component {
constructor(props){
super(props);
this.createSquare = this.createSquare.bind(this);
}
createSquare() {
let indents = [], rows = this.props.rows, cols = this.props.cols;
let squareSize = 50;
for (let i = 0; i < rows; i++) {
for (let j = 0; i < cols; j++) {
let topPosition = j * squareSize;
let leftPosition = i * squareSize;
let divStyle = {
top: topPosition+'px',
left: leftPosition+'px'
};
indents.push(<div style={divStyle}></div>);
}
}
return indents;
}
render() {
return (
<div>
{this.createSquare()}
</div>
);
}
}
export default Squares;
更新
@Ross Allen - 进行更改后,渲染方法似乎处于无限循环中,可能会导致内存崩溃