0

I'm in the process of learning React/javascript, so apologies if this is a simple question & answer. In short; I have a function within a React class that needs to be called on the success of an AJAX call (which is also nested inside another function in the same class).

Here is the example code:

var Bar = React.createClass({

    fooBar: function() {
        this.setState({
            step : this.state.step + 1
        })
    },

    submitRegistration: function() {
        $.ajax({
            url : "something",
            type: "POST",
            data : data,
            success: function(data, textStatus, jqXHR) {
                Bar.fooBar()
            }
        });
    }
}

I have tried the following with no success:

Bar.fooBar()
this.fooBar()

How can I call the fooBar() method from within the success AJAX method?

Donato Perconti
  • 814
  • 2
  • 11
  • 28
  • 1
    Take a look at http://stackoverflow.com/a/19274812/5111146 – James Brierley Apr 04 '16 at 15:44
  • 1
    I would suggest you have a services.js (or something like that) that returns a promise for your calls for you. You can temporarily bind `this` to something else (not recommend though), but basically your issue is that the context is incorrect at that point. You had the right idea with `this.fooBar`, also heads up - you named your function foorBar, not fooBar. – ajmajmajma Apr 04 '16 at 15:49
  • @JamesBrierley, That answered it. Thanks! – Donato Perconti Apr 04 '16 at 15:56

0 Answers0