1000ship / react-scroll-motion

🎞 Easy to make scroll animation
https://1000ship.github.io/react-scroll-motion
MIT License
422 stars 47 forks source link

Window is not defined after reload page on Next Js #18

Closed alimartondi closed 2 years ago

alimartondi commented 2 years ago

hello, I really like using your work. but I got error when using it for my Next Js project. When the website is rendered for the first time it runs normally, but when I refresh (reload) the error "window is not defined" appears, thank you

sereybuth120 commented 2 years ago

I ran into this issue as well. a while back. You can try doing this

const isClientRender = () => typeof window !== 'undefined' && typeof document !== 'undefined'; return <>{isClientRender && YOUR_CODE_GOES_HERE}</>

Hope this help.

TamjidAhmed10 commented 2 years ago

JUST ADD TO index.tsx if (typeof window == "undefined") { // browser code return(<h1></h1>) }

polopopeye commented 2 years ago

this is work for me hope it helps! 😄

import React, { useEffect, useState } from 'react';
import {
  Animator,
  ScrollContainer,
  ScrollPage,
  batch,
  Fade,
} from 'react-scroll-motion';

const ScrollSteps = () => {
  const [isBrowser, setIsBrowser] = useState(false);
  useEffect(() => {
    setIsBrowser(typeof window !== undefined ? true : false);
  }, []);

  return (
    <>
      {isBrowser && (
        <ScrollContainer>
          <ScrollPage page={0}>
            <Animator animation={batch(Fade())}>
              <h2>EXAMPLE1</h2>
            </Animator>
          </ScrollPage>
          <ScrollPage page={1}>
            <Animator animation={batch(Fade())}>
              <h2>EXAMPLE2</h2>
            </Animator>
          </ScrollPage>
          <ScrollPage page={2}>
            <Animator animation={batch(Fade())}>
              <h2>EXAMPLE3</h2>
            </Animator>
          </ScrollPage>
        </ScrollContainer>
      )}
    </>
  );
};

export default ScrollSteps;

I think this should be in the readme because I don't think it's a bug

Great work btw! I love the component

tofmat commented 2 years ago

Hey, instead of importing ScrollContainer from "react-scroll-motion" directly, you can import it like this.

const ScrollContainer = dynamic(
    () => import("react-scroll-motion").then((mod) => mod.ScrollContainer),
    { ssr: false }
  );

Worked for me!

alimartondi commented 2 years ago

Helpfully , thank you!

alimartondi commented 2 years ago

Thankyou everyone, your answers really helped me, even i'm confused which one to try! 😂

chouzzy commented 2 years ago

Worked for me as well! Thank you guys :)