Skip to content

Instantly share code, notes, and snippets.

@eduard-tkv
Last active March 22, 2017 17:36
Show Gist options
  • Save eduard-tkv/00cb9fa46c117bb8c48931be79ff4af3 to your computer and use it in GitHub Desktop.
Save eduard-tkv/00cb9fa46c117bb8c48931be79ff4af3 to your computer and use it in GitHub Desktop.
class Detail extends React.Component{
constructor(props){
super(props);
this.state = {
commits: [],
mode: "commits",
forks: [],
pulls: []
};
this.ajaxCall = this.ajaxCall.bind(this);
this.renderForks = this.renderForks.bind(this);
/*
var self = this;
this.ajaxCall('https://api.github.com/repos/facebook/react/commits', 'commits', function(response){
self.setState = {
commits: response,
mode: "commits",
forks: [],
pulls: []
};
console.log("sha from commits in constructor: " + response.commits[0].sha);
});
*/
}
ajaxCall(link, mode, callback){
ajax.get(link)
.end((error, response) => {
if (!error && response) {
var newStateObj = {};
newStateObj[mode] = response.body
newStateObj.mode = mode;
callback(newStateObj);
} else {
console.log('There was an error fetching from GitHub', error);
}
});
}
renderForks(){
var that = this;
this.ajaxCall('https://api.github.com/repos/facebook/react/forks', 'forks', function(newStateObj){
alert('that.state.mode inside callback: ' + that.state.mode); //alerts commits - as expected
alert('newStateObj.mode inside callback: ' + newStateObj.mode); //alerts forks - as expected
that.setState(newStateObj); // Error: setState is not a function
alert("this.state.forks: " + that.state.forks);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment