Skip to content

Instantly share code, notes, and snippets.

@cecigarcia
Created December 13, 2017 09:19
Show Gist options
  • Save cecigarcia/e5f19f1bf0d3fa388895994f2dac2371 to your computer and use it in GitHub Desktop.
Save cecigarcia/e5f19f1bf0d3fa388895994f2dac2371 to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
class MyComponent extends Component {
state = {
data: [],
error: null,
};
handleOnClick = () =>
fetchData()
.then(() => this.setState({ data }))
.catch(error => this.setState({ error }));
render() {
const { data, error } = this.state;
if (error) {
return (
<div className="error">
There was an error fetching data: {error}
</div>
);
}
return (
<div className="data">
<button onClick={this.handleClick}>reload data!</button>
<ul className="data-list">
{data.map((item, i) => <li key={i}>{item}</li>)}
</ul>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment