Last active
February 10, 2023 23:39
-
-
Save gtchakama/fc8055af6b08ab3d7cc8ab4835932b1e to your computer and use it in GitHub Desktop.
Fix : ReferenceError: window is not defined in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof window !== 'undefined') { | |
console.log('You are on the browser'); | |
// ✅ Can use window here | |
console.log(window.innerWidth); | |
window.addEventListener('mousemove', () => { | |
console.log('Mouse moved'); | |
}); | |
} else { | |
console.log('You are on the server'); | |
// ⛔️ Don't use window here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment