Domanda

I'm using ReactJS (by facebook) and I have the option of calling setState or setProps, but don't know which is more efficient performance-wise.

(Don't take my code on face value. This is just a simplification. My main concern is knowing which of setProps or setState is faster.)

For setProps I have:

function XHRcallback(data) {
  React.renderComponent(MainApp, $("container")).setProps(data);
}

For setState I have:

var updateAll;

function XHRcallback(data) {
  updateAll(data);
}

var List = React.createClass({
  getInitialState: function() {

    updateAll = function(data) {
      this.setState(data);
    }.bind(this);

  },

  render: function() {
    //stuff
  }
});
È stato utile?

Soluzione

State and props are equally efficient in React; they're used for different things. Think of props as inputs to your component, and state as internal, private variables.

See Thinking in React for a discussion of state vs. props.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top