最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Use setState or setProps is more efficient for Reactjs? - Stack Overflow

matteradmin3PV0评论

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
  }
});

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
  }
});
Share Improve this question asked Feb 24, 2014 at 6:17 AnusserAnusser 551 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

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

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

Post a comment

comment list (0)

  1. No comments so far