Skip to content

Instantly share code, notes, and snippets.

@chalisegrogan
Last active May 16, 2016 21:24
Show Gist options
  • Select an option

  • Save chalisegrogan/ac77c6be6cd0f1d61022c26474f6223f to your computer and use it in GitHub Desktop.

Select an option

Save chalisegrogan/ac77c6be6cd0f1d61022c26474f6223f to your computer and use it in GitHub Desktop.
Example of React & Fetch using ES6 syntax.
/*
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