dai-shi / waku

⛩️ The minimal React framework
https://waku.gg
MIT License
4.11k stars 108 forks source link

feat: expose hash from `useRouter` #746

Open pmelab opened 1 week ago

pmelab commented 1 week ago

What was changed?

Expose the current url hash via UNSTABLE_useRouter. On the server, its always an empty string, but gets populated on the client after the initial render.

Why was it changed?

Otherwise inconvenient useEffect-gymnastics are necessary to get the current hash reliably into a client component.

Notes

In the process of writing tests I found another potential issue with searchParams.

https://github.com/dai-shi/waku/pull/746/files#diff-a8dab8847d02ba73c6b66e738495556ddda5d2dd01b70322aae006f7669ea237R6-R11

I'm not sure if we should move the double-creation of URLSearchParams into the useRouter hook. Given some advice, I can try to fix that as well.

vercel[bot] commented 1 week ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
waku ✅ Ready (Inspect) Visit Preview Jun 14, 2024 5:34am
codesandbox-ci[bot] commented 1 week ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

pmelab commented 1 week ago

To be transparent, this is the code that we save in the consumer:

  const router = useRouter();
  const [hash, setHash] = useState('');
  useEffect(() => {
    setHash(window.location.hash);
  }, [router]);

Its not gigantic, but useEffect has a bad reputation lately.