Last active
May 16, 2016 21:24
-
-
Save chalisegrogan/ac77c6be6cd0f1d61022c26474f6223f to your computer and use it in GitHub Desktop.
Example of React & Fetch using ES6 syntax.
This file contains hidden or 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
| /* | |
| This is an example of how to properly call `bind` within `fetch` if referencing `this`. | |
| */ | |
| import React from 'react' | |
| class ExampleWithBind extends React.Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| prop_a: '' | |
| }; | |
| } | |
| componentDidMount() { | |
| fetch('example.json', { | |
| method: 'get' | |
| }).then(function(response) { | |
| return response.json() | |
| }).then(function(response) { | |
| this.setState({ prop_a: response.prop_a}); | |
| }.bind(this)); | |
| } | |
| render() { | |
| return ( | |
| <div>{this.state.prop_a}</div> | |
| ); | |
| } | |
| } | |
| export default ExampleWithBind; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment