Skip to content

Instantly share code, notes, and snippets.

@juhahinkula
Last active October 31, 2017 12:59
Show Gist options
  • Save juhahinkula/8de781104c4c448217d50b04d25cb0f5 to your computer and use it in GitHub Desktop.
Save juhahinkula/8de781104c4c448217d50b04d25cb0f5 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>React getting started</title>
</head>
<body>
<!-- Root container for react components -->
<div id='root'></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
<script type="text/babel">
class AsteroidList extends React.Component {
constructor(props) {
super(props);
this.state = {listItems: []};
}
componentDidMount() {
fetch('https://api.nasa.gov/neo/rest/v1/feed?start_date=2017-10-09&end_date=2017-10-09&api_key=DEMO_KEY')
.then((response) => response.json())
.then((responseData) => {
this.setState({
listItems: responseData.near_earth_objects["2017-10-09"],
});
})
}
render() {
const itemRows = this.state.listItems.map((asteroid) =>
<tr key={asteroid.name}>
<td>{asteroid.name}</td>
<td>{asteroid.close_approach_data[0].miss_distance.kilometers}</td>
</tr>
)
return (
<div>
<h2>Closest asteroids today</h2>
<table>
<tbody>
<tr><th>Name</th><th>Min distance</th></tr>
{itemRows}
</tbody>
</table>
</div>
);
}
}
ReactDOM.render(<AsteroidList />, document.getElementById('root'));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment