Skip to content

Instantly share code, notes, and snippets.

@abel-masila
Last active June 4, 2020 13:01
Show Gist options
  • Save abel-masila/560aacdcd919188d04262b46501cf127 to your computer and use it in GitHub Desktop.
Save abel-masila/560aacdcd919188d04262b46501cf127 to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from "react";
import axios from "axios";
function ViewPost(props) {
const [post, setPost] = useState({});
const id = props.match.params.id;
//OR const { id } = props.match.params;
useEffect(() => {
axios
.get(`https://jsonplaceholder.typicode.com/posts/${id}`)
.then((response) => {
console.log(response.data);
setPost(response.data);
})
.catch((err) => {
console.log(err);
});
});
return (
<div>
<h2>Post : {post.title}</h2>
</div>
);
}
export default ViewPost;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment