Skip to content

Instantly share code, notes, and snippets.

@andreasasprou
Last active September 12, 2018 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasasprou/4775e0e80866768d906b6e36909211c9 to your computer and use it in GitHub Desktop.
Save andreasasprou/4775e0e80866768d906b6e36909211c9 to your computer and use it in GitHub Desktop.
_getData = () => {
// Lets first set loading and disabled to be true
this.setState({ loading: true, disabled: true });
fetch('http://192.168.0.39/UserInfo/get_power_level.php')
//convert response to JSON format
.then((response) => response.json())
.then((responseJson) => {
// responseJson should be an object with a key "id", so we can access "id" by writing responseJson.id
console.log(responseJson);
// If we are here, then we successfully got responseJson from the server, lets now set the state
// also we will set loading and disbaled to be false
this.setState({ power_level: responseJson.id, loading: false, disabled: false });
})
.catch((error) => {
console.error(error);
// It's useful to store the error in the state so we can display it to the user if anything goes wrong.
this.setState({ loading: false, disabled: false, error: error });
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment