Skip to content

Instantly share code, notes, and snippets.

@dbani-dev
Last active July 10, 2017 04:57
Show Gist options
  • Save dbani-dev/52513f47c337cc21c13a23d297ca969a to your computer and use it in GitHub Desktop.
Save dbani-dev/52513f47c337cc21c13a23d297ca969a to your computer and use it in GitHub Desktop.
class ExampleComponent extends Component {
constructor() {
super();
this.state = {
data: null
}
}
// This method has an arrow function '=>', which binds 'this' to the method
// If 'this' isn't bound, this.setState won't be found/work
handleClick = () => {
// Get data from API
fetch('https://swapi.co/api/people/?page=1')
.then(res => res.json())
.then(jsonData => {
// Set jsonData to this.state.data
this.setState({
data: jsonData
})
});
}
render() {
return (
<div>
<button onClick={this.handleClick}>Get data</button>
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment