locomotivemtl / locomotive-scroll

🛤 Detection of elements in viewport & smooth scrolling with parallax.
https://locomotivemtl.github.io/locomotive-scroll
MIT License
7.56k stars 1.11k forks source link

Locomotive scroll with typescript #437

Open natainditama opened 2 years ago

natainditama commented 2 years ago

I'm using gatsby typescript to create a project. When I run local development it's fine, but when I run build genarate it's an error. That's because i use locomotive scroll using document object.

gambar

natainditama commented 2 years ago

I'm always frustrated when looking for such problems.

iojcde commented 2 years ago

This is because DOM APIs like document does not exist in a SSR environment. Try running the Locomotive Scroll init code inside a useEffect, so the code that accesses the browser only runs client-side.

natainditama commented 2 years ago

gambar As you can see, i use locomotive scroll init wrapper in useEffect

ghost commented 2 years ago

You have to check if document is defined. Then your build will not break.