React - Create nested components with loops

后端 未结 14 1779
情话喂你
情话喂你 2020-12-30 01:27

I have a little issue with React. I can\'t create a nested component with a for loop. What I want to do is create 9 cells of a table and then create 3 rows with 3 cells for

14条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 02:07

    Changed the code of the Board from the tutorial to reflect as

    class Board extends React.Component {
      constructor(props) {
        super(props);
        this.rows = Array(3).fill(null);
      }
      
      renderSquare(i) {
        return ( this.props.onClick(i)}
        />);
      }
      
      renderRow(row) {
        return (
          
    {this.rows.map((x, col) => this.renderSquare(3*row+col))}
    ); } render() { return (
    {this.rows.map((x, rowIndex) => this.renderRow(rowIndex))}
    ); } }

提交回复
热议问题