purpose of .bind(this) at end of ajax callback?

后端 未结 2 545
离开以前
离开以前 2021-01-30 04:07

From the reactjs tutorial, what\'s the purpose of having .bind(this) at the end of the ajax callback? Does code work correctly without it?

        d         


        
2条回答
  •  孤城傲影
    2021-01-30 04:29

    The purpose of having .bind(this) at the end of the ajax callback is let this be related to your react class. In other words you can add:

    var self = this;
    

    outside of ajax and it works the same. You code equal to:

    var self = this;
    $.ajax({
        .
        .
        data: JSON.stringify({text: text}),
        success: function (data) {
            self.setState({data: data});
        },
        .
        .
    });
    

提交回复
热议问题