Last active
October 24, 2018 15:46
-
-
Save pomber/4d551d82a02a9c261af6e1552d33433b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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