Skip to content

Commit 865b1be

Browse files
committed
Add useState to Post to add like button
1 parent 8c4c8de commit 865b1be

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/components/Post.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
import React from "react";
1+
import React, { useState } from "react";
22
import PropTypes from "prop-types";
3+
import "../styles/post.css";
34

45
const Post = ({postData}) => {
5-
const {title, author, date, isPublished, body, tags} = postData
6+
const {title, author, date, isPublished, body, tags} = postData;
7+
const [count, setCount] = useState(0);
8+
9+
const handleClick = () => {
10+
setCount(prev => prev + 1)
11+
}
612

713
return (
814
<div className="post">
915
<div className="post-heading">
1016
<h2>{title}</h2>
1117
{isPublished ? <p>{body}</p> : <p>Coming soon!</p>}
1218
</div>
19+
<div className="post-counter">
20+
<span>Upvotes: {count}</span>
21+
<button onClick={handleClick} type="button">Upvote this</button>
22+
</div>
1323
<div className="post-author">Author: {author}</div>
1424
<div className="post-date">Published: {date}</div>
1525
<h3>Tags:</h3>

src/styles/post.css

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.post-counter span {
2+
margin-right: 1rem;
3+
}
4+
5+
.post-counter,
6+
.post-author {
7+
margin-bottom: 0.5rem;
8+
}

0 commit comments

Comments
 (0)