streamich / react-use

React Hooks — 👍
http://streamich.github.io/react-use
The Unlicense
41.98k stars 3.16k forks source link

window being used outside client side hook leads to server side ReferenceError Next.js 14 #2594

Closed asasvirtuais closed 2 months ago

asasvirtuais commented 2 months ago

What is the current behavior?

⨯ ReferenceError: window is not defined
GET /page 500

Steps to reproduce it and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have extra dependencies other than react-use. Paste the link to your JSFiddle or CodeSandbox example below:

https://codesandbox.io/p/devbox/strange-dawn-ngvwfs

'use client'
import { useHash } from 'react-use'

export default function () {
  try {
    useHash()
  } catch (err: any) {
    return <pre>{err.message}</pre>
  }
}

What is the expected behavior? window to be accessed in the client side.

A little about versions:

asasvirtuais commented 2 months ago

Draft PR: https://github.com/streamich/react-use/pull/2595

asasvirtuais commented 2 months ago

The rabbit hole is deeper than I imagined it'd be. Seems like useEffect would be the right approach to solve this issue with Next.js, but it would completely change the way this works on React only apps.