1

My objective is to create the following html table from a list of rows ([1, 2, 3]) and a list of columns ([1, 2]) using ReactJS:

<table>
<tr>
 <td>1</td>
 <td>2</td>
 </tr>
 <tr>
  <td>1</td>
 <td>2</td>
 </tr>
  <tr>
   <td>1</td>
   <td>2</td>
  </tr>
</table>

See below for my React script and here for my codepen, which doesnt seem to be working

 class Tbody extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          columns: [1, 2],
          rows: [1, 2, 3]
        };
      }

      renderCols() {
        return (
          {this.state.columns.map(col => <td key={col}> {col} </td>)}
        );
      }

      renderRows(){
       return (
         {this.state.rows.map(row => <tr key={row}> {this.renderCols()} </tr>)}
        );
      }

      render() {
        return <tbody>{this.renderRows()}</tbody>;
      }
    }

    class Table extends React.Component {
      render() {
        return (
          <div>
            <table>
            <Tbody />
             </table>
          </div>
        );
      }
    }

    ReactDOM.render(<Table />, document.getElementById("root"));

1 Answer 1

3

Your renderCols and renderRows method returning the JSX. Instead return from there just plain JS objects, remove those {..}.

class Tbody extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      cols: [1, 2],
      rows: [1, 2, 3]
    };
  }

  renderCols() {
    
    return (
      this.state.cols.map(col => <td key={col}>{col}</td>)
    );
  };

  renderRows(){
   return (
     this.state.rows.map(row => <tr key={row}>{this.renderCols()}</tr>)
    );
  }
  
  render() {
    return <tbody>{this.renderRows()}</tbody>;
  }
}

class Table extends React.Component {
  render() {
    return (
      <div>
        <table>
        <Tbody />
         </table>
      </div>
    );
  }
}

ReactDOM.render(<Table />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

This works but this codepen console with your update(codepen.io/chriscruz/pen/aPwbNP?editors=1011) is giving me a warning in the console: Warning: validateDOMNesting(...): %s cannot appear as a child of <%s>.%s%s%s" "Whitespace text nodes" "tr" " Make sure you don't have any extra whitespace between tags on each line of your source code." "" "
@Chris Remove spaces between <tr></tr> which you have. Like <tr key={row}> {this.renderCols()} </tr> should be <tr key={row}>{this.renderCols()}</tr>.. Same goes for <td>. React warns those.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.