DEV Community

Homyee King
Homyee King

Posted on

How to import a npm package which not support SSR?

I've tried use dynamic import import() in useEffect, like

// app.js
import {useEffect} from 'react'
function MyButton() {
  const [Comp, setComp] = useState();
  useEffect(() => {
    //  setComp(lazy(import("./test")))
    import("./test").then((mod) => {
      setComp(mod.default);
    });
  }, []);
  return Comp ? <Comp /> : null;
}

// test.js

import { useMemo } from "react";

export default () => {
  const title = useMemo(()=>'123',[])
  return <div>{213}</div>;
};

Enter fullscreen mode Exit fullscreen mode

but if the importee use react hooks internaly, the application will crash

Image description

because can't use hooks inside of useEffect

Top comments (0)