preline icon indicating copy to clipboard operation
preline copied to clipboard

Cannot read properties of undefined (reading 'autoInit')

Open Slddev opened this issue 2 years ago • 10 comments

image

Slddev avatar Jan 26 '24 10:01 Slddev

Also seeing this

ollebergkvist avatar Jan 28 '24 02:01 ollebergkvist

Also seeing this

luguanhui avatar Jan 28 '24 03:01 luguanhui

I'm facing the same issue.

jordankimsey avatar Jan 29 '24 14:01 jordankimsey

I see many of us are getting confused by HSStaticMethods. It's just a way of saying that you need to import HS (eg.: HSAccordion) and call autoInit on its instance.

Here's the PrelineLoader.jsx from one of my (Next.js) projects:

"use client";

import HSAccordion from "@preline/accordion";
import { usePathname } from "next/navigation";
import { HSDropdown } from "preline/preline";
import { useEffect } from "react";

export default function PrelineLoader() {
  const path = usePathname();

  useEffect(() => {
    import("preline/preline");
  }, []);

  useEffect(() => {
    setTimeout(() => {
      HSAccordion.autoInit();
      HSDropdown.autoInit();
    }, 100)
  }, [path])

  return <></>
}

I hope this helps. :)

VishalLudhrani avatar Feb 02 '24 18:02 VishalLudhrani

@VishalLudhrani So do I need to add HSDropdown instance if I want to ad the dropdown components? I'm still not getting them to work.

Wigglor avatar Feb 13 '24 10:02 Wigglor

Try setTimeout to 200? or something like this

useEffect(() => {
  if (document.readyState === 'complete') {
    window.HSStaticMethods.autoInit()
  }
}, [path])

MeromTh avatar Feb 14 '24 09:02 MeromTh

image

Hi! Please check out the official documentation here Preline - Next.js You should add the following code to prevent TS warnings:

import { IStaticMethods } from "preline/preline";
declare global {
  interface Window {
    HSStaticMethods: IStaticMethods;
  }
}

olegpix avatar Feb 28 '24 16:02 olegpix

@VishalLudhrani So do I need to add HSDropdown instance if I want to ad the dropdown components? I'm still not getting them to work.

Yup. That is how it worked for me.

If you weren't able to get those to work, here's an alternative approach:

"use client";

import { usePathname } from "next/navigation";
import { useEffect } from "react";

const isBrowser = typeof window !== undefined;

export default function PrelineLoader() {
  const path = usePathname();

  useEffect(() => {
    if (isBrowser) {
      // if this component is rendered on a browser, import preline
      import("preline/preline");
    }
  }, [])

  useEffect(() => {
    setTimeout(() => {
      if (isBrowser) {
        // if this component is rendered on a browser, import relevant preline plugins
        import("preline/preline").then(({ HSAccordion, HSDropdown, HSCollapse }) => {
          HSAccordion.autoInit();
          HSDropdown.autoInit();
          HSCollapse.autoInit();
        })
      }
    }, 100)
  }, [path])

  return <></>
}

@Wigglor

VishalLudhrani avatar Mar 01 '24 15:03 VishalLudhrani

I see many of us are getting confused by HSStaticMethods. It's just a way of saying that you need to import HS (eg.: HSAccordion) and call autoInit on its instance.

Here's the PrelineLoader.jsx from one of my (Next.js) projects:

"use client";

import HSAccordion from "@preline/accordion";
import { usePathname } from "next/navigation";
import { HSDropdown } from "preline/preline";
import { useEffect } from "react";

export default function PrelineLoader() {
  const path = usePathname();

  useEffect(() => {
    import("preline/preline");
  }, []);

  useEffect(() => {
    setTimeout(() => {
      HSAccordion.autoInit();
      HSDropdown.autoInit();
    }, 100)
  }, [path])

  return <></>
}

I hope this helps. :)

Hi can you please explain how HSAccordion is related to HSStaticMethods?

insivika avatar Mar 07 '24 15:03 insivika

@insivika HSStaticMethods could be a placeholder (just a way to say your plugin would go here). You would need to replace it with all the plugins you're using in your project.

So, in my case, I replaced it with HSAccordion and HSDropdown since I was using both.

[EDIT]: This was just my assumption, which seemed to work for me.

VishalLudhrani avatar Mar 08 '24 17:03 VishalLudhrani

I see many of us are getting confused by HSStaticMethods. It's just a way of saying that you need to import HS (eg.: HSAccordion) and call autoInit on its instance. Here's the PrelineLoader.jsx from one of my (Next.js) projects:

"use client";

import HSAccordion from "@preline/accordion";
import { usePathname } from "next/navigation";
import { HSDropdown } from "preline/preline";
import { useEffect } from "react";

export default function PrelineLoader() {
  const path = usePathname();

  useEffect(() => {
    import("preline/preline");
  }, []);

  useEffect(() => {
    setTimeout(() => {
      HSAccordion.autoInit();
      HSDropdown.autoInit();
    }, 100)
  }, [path])

  return <></>
}

I hope this helps. :)

Hi can you please explain how HSAccordion is related to HSStaticMethods?

This Code is work for me ✨

Nishitbaria avatar Mar 27 '24 03:03 Nishitbaria

Hey folks, seems the issue has been addressed by the community. We've also added Preline JavaScript page with some explanation how Preline JavaScript works.

https://preline.co/docs/preline-javascript.html

Cheers!

jahaganiev avatar Apr 03 '24 23:04 jahaganiev