What is bind(this)?

Asked 1 years ago, Updated 1 years ago, 58 views

While doing the React.js tutorial (https://facebook.github.io/react/docs/tutorial.html), I got the following code:

//tutorial13.js
varCommentBox=React.createClass({
  getInitialState: function(){
    return {data:[]};
  },
  componentDidMount: function() {
    $.ajax({
      url —This.props.url,
      dataType: 'json',
      success:function(data){
        This.setState({data:data});
      }.bind(this),
      error: function(xhr, status, error) {
        console.error(this.props.url, status, err.toString());
      }.bind(this)
    });
  },
  render: function() {
    return(
      <div className="commentBox">
        <h1>Comments</h1>
        <CommentList data={this.state.data}/>
        <CommentForm/>
      </div>
    );
  }
});

What does bind(this) mean here?

javascript jquery ajax reactjs

2022-09-29 21:45

1 Answers

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

It was written in the example shown in .


2022-09-29 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.