I'm learning React, stumbling around trying to find the right way to do the things I want to do.
So I have a method that gets called in the constructor - let's call this getData(). This method goes out and obtains data from an external resource via a jQuery Ajax call (I may use Fetch or something else at some point, but that's not the issue), then applies some logic in the data, using the member function formatData(). The call looks something like this:
class MyClass extends React.Component {
// .. constructor, render, etc code
getData() { // Part of the MyClass class.
var self = this;
var effUrl = 'http://someurl';
return $.ajax({
url: effUrl,
method: 'GET',
appendMethodToURL: false
})
.done(function (data) {
self.setState( { effString: self.formatData(data['results']) } );
})
.fail(function (errorHandler, soapResponse, errorMsg) {
alert('Error');
})
.always(function () {
alert('Always');
});
}
formatData() {
// format the returned data...
}
}
While the code works, it is because of a hack of assigning 'this' to 'self'. Since in my code when done is called 'this' is something different (probably the returned promise object). What is the proper 'React' way to solve this problem - in essence, call a function that is a member of the component from inside of a data call?
tl;dr I would like to replace my use of 'self' in my working code with something that was a better approach.