1

I'm trying to implement Locomotive Scroll in my Nextjs app, and I'm having trouble...

Here's my code:

import type { AppProps } from "next/app";
import { useEffect } from "react";

import "../styles/global.css";
import "../styles/locomotive-scroll.css";

import locomotiveModule from "locomotive-scroll";

function MyApp({ Component, pageProps }: AppProps) {
  useEffect(() => {
    let scroll: any;

    if (typeof window !== "undefined")
      scroll = new locomotiveModule.default({
        el: document.querySelector("[data-scroll-container]"),
        smooth: true,
        smoothMobile: false,
        resetNativeScroll: true,
      });

    // `useEffect`'s cleanup phase
    return () => scroll.destroy();
  });

  return (
    <main className="main" data-scroll-container>
      <Component {...pageProps} />
    </main>
  );
}

export default MyApp;

This throughs a document is not defined error message and I don't understand why... I look up for that error and with the if (typeof window !== "undefined") should work, but is not working at all.

Thanks!

Emiliano
  • 437
  • 1
  • 4
  • 14
  • Does this answer your question: [How to correctly use Locomotive Scroll with Next.js routing?](https://stackoverflow.com/questions/68264720/how-to-correctly-use-locomotive-scroll-with-next-js-routing)? You need to dynamically import `locomotive-scroll` inside the `useEffect`. – juliomalves May 13 '22 at 22:58

0 Answers0