Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window is not defined (SSR) #827

Closed
nicksrandall opened this issue Nov 7, 2022 · 4 comments
Closed

window is not defined (SSR) #827

nicksrandall opened this issue Nov 7, 2022 · 4 comments

Comments

@nicksrandall
Copy link

Would it be possible to make this hook SSR compatible by wrapping this fn in a guard to only run on the client?

window.addEventListener('DOMContentLoaded', () => {

@JohannesKlauss
Copy link
Owner

Yes, I'll fix that later this day.

@JohannesKlauss
Copy link
Owner

Fixed in 4.0.3

@nicksrandall
Copy link
Author

nicksrandall commented Nov 8, 2022

@JohannesKlauss I'm still getting an issue when running server-side because of the usage of useLayoutEffect which is not supported in a server environment. I've seen many other libraries get around this issue by creating an abstraction called useSafeLayoutEffect which will run a regular useEffect on server and useLayoutEffect on client.

See: https://github.com/chakra-ui/chakra-ui/blob/260076e2ac6165c8079c800475b9d7a9ca0f29a2/packages/components/descendant/src/utils.ts#L53-L54

nicksrandall added a commit to nicksrandall/react-hotkeys-hook that referenced this issue Nov 8, 2022
This will prevent React from complaining when `useLayoutEffect` is run on server. See JohannesKlauss#827
@JohannesKlauss JohannesKlauss reopened this Nov 8, 2022
@JohannesKlauss
Copy link
Owner

This should be fixed with version 4.0.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants