Last active
March 22, 2017 17:36
-
-
Save eduard-tkv/00cb9fa46c117bb8c48931be79ff4af3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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