Skip to content

Instantly share code, notes, and snippets.

@ddgromit
Last active September 28, 2015 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddgromit/c7889859265491b26706 to your computer and use it in GitHub Desktop.
Save ddgromit/c7889859265491b26706 to your computer and use it in GitHub Desktop.
// WeatherPage.js
class WeatherPage extends React.Component {
constructor() {
this.state = {
loading: true,
temperature: null,
error: null,
}
}
componentWillMount() {
axios.get('http://api.some-weather-service.com/temperature').then((data) => {
this.setState({
temperature: data.temperature,
loading: false
});
}).catch((data) => {
this.setState({
error: "We had a problem requesting temperature data",
loading: false
});
});
}
render() {
let content;
if (this.loading) {
content = (
<div>
Loading...
</div>
);
} else if (this.error !== null) {
content = (
<div className="alert alert-danger" role="alert">
<span className="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
{this.error}
</div>
);
} else {
<p>
{ this.state.temperature }
</p>
}
return (
<div>
<h1>Weather</h1>
<h2>Todays temperature</h2>
{ content }
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment