4lejandrito / next-plausible

Simple integration for https://nextjs.org and https://plausible.io analytics
https://next-plausible.vercel.app
MIT License
603 stars 33 forks source link

Wrap usePlausible in useCallback #38

Closed Merott closed 2 years ago

Merott commented 2 years ago

usePlausible returns a new function every time, which makes it unsuitable to use in the dependency array of other react hooks.

For now, I'm working around it with a custom wrapper, but it's not ideal:

import { usePlausible as useNextPlausible } from 'next-plausible'
import { useCallback } from 'react'

export function usePlausible() {
  const plausible = useNextPlausible()

  // disabling deps rule because the upstream implementation never changes
  // but it isn't set up with useCallback, so every time it's a new function

  // eslint-disable-next-line react-hooks/exhaustive-deps
  return useCallback(plausible, [])
}
4lejandrito commented 2 years ago

Thanks a lot for reporting this @Merott!. Fixed in https://github.com/4lejandrito/next-plausible/pull/39.

4lejandrito commented 2 years ago

Published as next-plausible@3.1.6.

Merott commented 2 years ago

Nice! Thanks for taking care of it so quickly! 👍🏻