`window.scrollY` error `typescript`

I'm trying to get the position of the webpage and use it as a condition to run some command if the condition is met.
const getScroll = () => {
const check = window.scrollY; // If I set the type here to HTMLElement the condition below here becomes invalid because check can't be compared to a number.
if (check >= 100) {
setScroll(true);
} else {
setScroll(false);
}
};
window.addEventListener("scroll", getScroll);
const getScroll = () => {
const check = window.scrollY; // If I set the type here to HTMLElement the condition below here becomes invalid because check can't be compared to a number.
if (check >= 100) {
setScroll(true);
} else {
setScroll(false);
}
};
window.addEventListener("scroll", getScroll);
2 Replies
Piotrek
Piotrek2y ago
You basically have to check if the windows is defined and then use window in the function. It can be undefined while rendering on server.
if (typeof window !== undefined) {
...
}
if (typeof window !== undefined) {
...
}
Leke
LekeOP2y ago
Thanks
Want results from more Discord servers?
Add your server