react-feather#Heart TypeScript Examples

The following examples show how to use react-feather#Heart. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: likebutton.tsx    From samuelkraft-next with MIT License 6 votes vote down vote up
LikeButton = ({ slug }: { slug: string }): JSX.Element | null => {
  const [mounted, setMounted] = useState(false)
  const { data } = useSWR(`/api/likes?slug=${slug}`, fetcher)
  const likes = data?.likes
  const liked = localStorage.getItem(slug) === 'true'

  useEffect(() => setMounted(true), [])

  const onLike = async () => {
    localStorage.setItem(slug, 'true')
    mutate(`/api/likes?slug=${slug}`, { ...data, likes: likes + 1 }, false)
    await fetch(`/api/likes?slug=${slug}`, { method: 'POST' })
  }

  if (!mounted) return null

  return (
    <Button disabled={liked} onClick={onLike} type="button" variant="like">
      <Heart className={liked ? styles.icon : ''} /> {typeof likes === 'undefined' ? '--' : likes}
    </Button>
  )
}