cantera
cantera

Reputation: 25005

React JSX: Cache Props?

When accessing the same props value multiple times in React/JSX, is it advisable to cache the object in a local variable?

var ItemComponent = React.createClass({

  render: function() {

    var cached = this.props.item;

    return (
      <div className={cached.class}>
        <h1>{cached.heading}</h1>
        <p>{cached.text}</p>
      </div>
    );
  }
});

Upvotes: 3

Views: 2525

Answers (2)

Michael LaCroix
Michael LaCroix

Reputation: 5808

The props are just properties on a JavaScript object – not getter functions, so there shouldn't be any noticeable difference in performance.

Upvotes: 5

Sophie Alpert
Sophie Alpert

Reputation: 143134

If you find it more convenient, you're free to do that but there's little to no performance benefit from doing so. Object property accesses are generally very fast.

Upvotes: 3

Related Questions