Created
November 15, 2021 14:48
-
-
Save catwhocode/e3a674ba63e72949df44b4e98af75c5a to your computer and use it in GitHub Desktop.
Notes from Prawito Hudoro's React Tutorial | Session 11
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, {Component, Fragment} from 'react' | |
import Post from '../../component/Post/Post' | |
import axios from 'axios'; | |
class BlogPost extends Component { | |
state = { | |
post: [] | |
} | |
handleFetch = (url) => { | |
fetch(url) | |
.then(response => response.json()) | |
.then(json => { | |
this.setState({ | |
post: json | |
}) | |
}) | |
} | |
handleAxios = (url) => { | |
axios.get(url) | |
.then((response) => { | |
this.setState({ | |
post: response.data | |
}) | |
}) | |
.catch((error) => { | |
console.log(error) | |
}) | |
} | |
componentDidMount(){ | |
const url = 'http://localhost:8000/posts?_expand=author' | |
// this.handleFetch(url) | |
this.handleAxios(url) | |
} | |
render(){ | |
return( | |
<Fragment> | |
<h1>Blog Post</h1> | |
{ | |
this.state.post.map(post => { | |
return <Post key={post.id} title={post.title} description={post.description} /> | |
}) | |
} | |
</Fragment> | |
) | |
} | |
} | |
export default BlogPost |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<title>Learn React</title> | |
</head> | |
<body> | |
<div id="root"></div> | |
</body> | |
</html> |
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' | |
import BlogPost from './container/BlogPost/BlogPost' | |
ReactDOM.render(<BlogPost />, document.getElementById('root')) |
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
.post { | |
display: inline-block; | |
border: solid 1px silver; | |
padding: 5px; | |
margin-right: 5px; | |
} | |
.title { | |
font-weight: bold; | |
} |
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 './Post.css' | |
const Post = (props) => { | |
return ( | |
<div className="post"> | |
<div className="img-thumb"> | |
<img src="uploads/1.jpg" alt="Obama" /> | |
</div> | |
<div className="content"> | |
<p className="title">{props.title}</p> | |
<p className="desc">{props.description}</p> | |
</div> | |
</div> | |
) | |
} | |
export default Post |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment