Reputation: 11
EXAMPLE "BIND" from ReactJS code. I have never used bind and not sure what it does on an ajax call like in the code below.
React.createClass({
componentWillMount: function () {
$.get(this.props.url, function (data) {
this.setState(data);
}.bind(this));
},
render: function () {
return <CommentList data={this.state.data} />
}
});
Upvotes: 0
Views: 50
Reputation: 318182
It doesn't specifically do anything to ajax calls, it binds the this-value for any function it's used on.
From MDN
The
bind()
method creates a new function that, when called, has itsthis
keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
A quick example
function doStuff() {
console.log(this); // would print "hello kitty"
}
var fn = doStuff.bind('Hello Kitty'); // set "this", then return new function
fn(); // call with given "this" value
The code in the question simple sets the this
value inside the $.get
functions callback, to the same this
value as componentWillMount()
Upvotes: 1