React - Create nested components with loops

后端 未结 14 1536
情话喂你
情话喂你 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:18

    I will assume you are looking at the React Tutorial example.. As the instructions explicitly point out, the idea is to make two loops, not just the one.

    The first loop in this example creates the 3 rows. The second nested loop creates the 3 necessary columns, returning a Square for each position through the renderSquare function. You may notice that I use the indexes of both loops to correctly assign the corresponding index to the square.

    return (
        <div>
          {[0,1,2].map(i => {
            return (
              <div className="board-row">
                {[0,1,2].map(j => {
                  return this.renderSquare(3*i + j)
                })}
              </div>
            );
          })}
        </div>
      );
    
    0 讨论(0)
  • 2020-12-30 02:22

    You are pushing functions to the board array. If you want render them, you have to call those functions like

    {board[0]()}
    

    I prepared an example that covers your problem: http://jsbin.com/hofohiy/edit?js,output

    0 讨论(0)
提交回复
热议问题