Skip to content

Instantly share code, notes, and snippets.

@pomber
Last active October 24, 2018 15:46
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 pomber/4d551d82a02a9c261af6e1552d33433b to your computer and use it in GitHub Desktop.
Save pomber/4d551d82a02a9c261af6e1552d33433b to your computer and use it in GitHub Desktop.
import React from "react";
import ReactDOM from "react-dom";
class Post extends React.Component {
state = {
loading: true,
post: null
};
componentDidMount() {
const { postId } = this.props;
fetch("https://jsonplaceholder.typicode.com/posts/" + postId)
.then(response => response.json())
.then(post => this.setState({ post, loading: false }));
}
render() {
const { loading, post } = this.state;
if (loading) {
return <div>Loading...</div>;
}
return (
<div>
<h1>{post.title}</h1>
<p>{post.body}</p>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<Post postId={1} />, rootElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment