-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathHeart.js
More file actions
31 lines (25 loc) · 884 Bytes
/
Heart.js
File metadata and controls
31 lines (25 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React, { useState, useEffect } from 'react';
function Heart (props) {
const [ likeCount, setLikeCount ] = useState(0);
useEffect(() => {
getLikeCountForMentor(props.name);
// eslint-disable-next-line
}, []);
function getLikeCountForMentor (mentorName) {
const mentorLikeCount = localStorage.getItem(`${mentorName}_likeCount`);
if (mentorLikeCount) {
setLikeCount(parseInt(mentorLikeCount));
}
}
function setLikeCountToLocalStorage () {
setLikeCount(likeCount + 1)
localStorage.setItem(`${props.name}_likeCount`, likeCount + 1);
}
return (
<div className='heart-container' onClick={setLikeCountToLocalStorage}>
<div className='heart'>♥</div>
<div className='like-count'>{likeCount}</div>
</div>
);
}
export default Heart;