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

ReferenceError: document is not defined #42

Open
3 tasks done
aliskhanoff opened this issue Jan 21, 2024 · 3 comments
Open
3 tasks done

ReferenceError: document is not defined #42

aliskhanoff opened this issue Jan 21, 2024 · 3 comments

Comments

@aliskhanoff
Copy link

Environment:

  • nextjs
  • node: v21.5.0
  • react: 18+

I am trying to conditionally render component when the user wants to play a game
const [playTetris, setPlayTetris] = useState(false);
and conditional render
{ playTetris && <Tetris />) }

At the dev environment - doesn't make any problem.

While Building:

ReferenceError: document is not defined

Probably, the Tetris component trying to access the window.document before ComponentDidMount?

@brandly
Copy link
Owner

brandly commented Mar 10, 2024

Hey, sorry I just noticed this issue.

I see you're using nextjs. Are you using the app/ router? I haven't tried this lib in that kind of server-rendered environment

This is the only place document gets touched, so I guess doing it at the module level is causing trouble

document.addEventListener('keydown', (e) => {
if (e.shiftKey && !isPressed) {
isPressed = e.shiftKey;
callCallbacks();
}
return true;
});
document.addEventListener('keyup', (e) => {
if (!e.shiftKey && isPressed) {
isPressed = e.shiftKey;
}
return true;
});

@ImranSefat
Copy link

Any idea on how to fix this issue? I am trying to run this on an expo native react project, thought at first it might be a problem with the dom-manipulation as I'm running this with expo go in my iPhone.

@brandly
Copy link
Owner

brandly commented Mar 29, 2024

@ImranSefat want to file a separate issue about supporting react native? It seems like in that environment, you wouldn't want any keyboard handling, ticks would be handled with Timers instead of window, etc. It'd be a bit more work

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

3 participants