Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use setState or setProps is more efficient for Reactjs?

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
  }
});
like image 768
Anusser Avatar asked Dec 16 '22 01:12

Anusser


1 Answers

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.

like image 94
Sophie Alpert Avatar answered Jan 18 '23 03:01

Sophie Alpert