Skip to content

Instantly share code, notes, and snippets.

@gre
Created May 25, 2016 19:24
Show Gist options
  • Save gre/01b4d5b9bd7262f1dd3998b858155492 to your computer and use it in GitHub Desktop.
Save gre/01b4d5b9bd7262f1dd3998b858155492 to your computer and use it in GitHub Desktop.
class Foo extends Component {
constructor () {
super();
this.state = {
data: null,
error: null,
pending: false
};
}
fetch = () => {
if (this.state.pending) return; // don't fetch if already pending
this.setState({ pending: true });
$.ajax(...)
.then(data => this.setState({
data,
pending: false
}))
.catch(error => this.setState({
error,
pending: false
}))
};
render () {
const { data, error } = this.state;
return <div>
<button onClick={this.fetch}>
Fetch
</button>
{ data && <Data data={data} /> }
{ error && <Error error={error} /> }
</div>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment