-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
ScrollReveal not working with Next JS (create-next-app) ? #541
Comments
This issue is happening because the DOM is only available inside the browser client side and not server side. Nextjs is executing your code server side. To get around the error you can leverage nextjs dynamic imports feature and a react useEffect hook. This will ensure the DOM is available before importing scrollreveal const MyComponent = () => {
const refToComponent = React.useRef(null)
useEffect(() => {
async function animate() {
if (refToComponent.current) {
const sr = (await import("scrollreveal")).default
sr().reveal(refToComponent.current)
}
}
animate()
}, [])
return (
<div ref={refToComponent}>HEY THERE!</div>
)
} Hope this helps 😄 |
If anyone facing the same issue, I have created a helper package to use Scrollreveal with Next.js. You can check out here. |
use sr().reveal(refToComponent.current, {reset: true}), scroll back to the revealed element, the animation can't animate again, const MyComponent = () => {
const refToComponent = React.useRef(null)
useEffect(() => {
async function animate() {
if (refToComponent.current) {
const sr = (await import("scrollreveal")).default
sr().reveal(refToComponent.current, {reset: true})
}
}
animate()
}, [])
return (
<div ref={refToComponent}>HEY THERE!</div>
)
} |
Hey @MQDXL , due to the nature of Next13/React 18 (Now React is a server side lib) -> you cannot grab elements from the DOM without specifying the |
@ritmillio thanks bro, I found my code error.that's the element's parent set height: 100%. say thanks to you again . |
I have some issues using ScrollReveal with create-next-app.
I installed ScrollReveal with npm, then imported it using ES6 :
import sr from 'scrollreveal';
I immediatly have an error message :
Server Error
ReferenceError: document is not defined
I use ScrollReveal with create-react-app, and it works perfectly. So I don't understand.
Thanks.
Environment
The text was updated successfully, but these errors were encountered: