Created
December 13, 2017 09:19
-
-
Save cecigarcia/e5f19f1bf0d3fa388895994f2dac2371 to your computer and use it in GitHub Desktop.
This file contains 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
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