0

I am still in progress of learning Reactjs.

I am trying to populate table headers within a table row.

Somehow the code written below is treating the generated table headers via the renderTableHeaders() function as pure text only.

module.exports = React.createClass({

  getInitialState: function() {
    return {
      defaultTableHeaders: [
        'a', 'b', 'c', 'd',
      ]
    }
  },

  renderTableHeaders: function() {

      var markup = [];

      var defaultTableHeaders = this.state.defaultTableHeaders;

      for (var index = 0; index < defaultTableHeaders.length; index++) {
        markup.push('<th>' + defaultTableHeaders[index] + '</th>');
      }

      return markup;
  },

  render: function() {

    return (
      <thead>
        <tr>
          {this.renderTableHeaders()}
        </tr>
      </thead>
    );
  }

});

When I modify the render() function to below then it would work properly. Any idea why?

  render: function() {

    return (
      <thead>
        <tr>
          <th>a</th><th>b</th><th>c</th><th>d</th>
        </tr>
      </thead>
    );
  }

3 Answers 3

2

Use jsx syntax to create and push elements to the array:

markup.push(<th>{defaultTableHeaders[index]}</th>);
Sign up to request clarification or add additional context in comments.

1 Comment

ouch, the solution is as simple as that. I didn't realize I couldn't treat the markup as string. Thank you
2

Yes, so in your renderTableHeaders() you're returning an array. So when you do: {this.renderTableHeaders()} its really just a javascript array with all the tags you need. Try using {markup} instead of calling the function directly. Check out this answer here: loop inside React JSX

Comments

1

The main issue is that your renderTableHeaders function returns a string instead of returning actual JSX markup.

Please try this:

module.exports = React.createClass({
  getInitialState: function() {
    return {
      defaultTableHeaders: [
        'a', 'b', 'c', 'd',
      ]
    }
  },

  renderTableHeaders: function() {
    var defaultTableHeaders = this.state.defaultTableHeaders;

    return defaultTableHeaders.map(function (header) {
      return (
        <th>{header}</th>
      );
    })
  },

  render: function() {
    return (
      <thead>
        <tr>
          {this.renderTableHeaders()}
        </tr>
      </thead>
    );
  }
});

2 Comments

What are the differences between creating a for loop and push each markup statement into an array in my original code versus using the map() function in the renderTableHeaders() function? Thanks
Since map returns a transformed version of your original array it's more suitable for this type of cases, it requires less code and it looks a bit more readable. Plus, is the recommended way in React samples: facebook.github.io/react/docs/…

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.