uPlot icon indicating copy to clipboard operation
uPlot copied to clipboard

How to use uPlot with NextJS

Open coditori opened this issue 5 years ago • 4 comments

Hi, I just saw the react sample and it works fine but with next I got this error "navigator is not defined", dynamic import could not help me either.

coditori avatar Feb 01 '21 13:02 coditori

think you gotta wait until mount and init uPlot in a useEffect hook?

https://github.com/vercel/next.js/discussions/11027

leeoniya avatar Feb 01 '21 13:02 leeoniya

@leeoniya Yes because we need to access uplot constructor

coditori avatar Feb 01 '21 15:02 coditori

i'm not familiar with nextjs. does this solve your issue?

leeoniya avatar Feb 01 '21 15:02 leeoniya

After a lot of research this is what worked for me, see the discussion here next js issue report

import React, { useEffect, useRef } from "react";
import "/node_modules/uplot/dist/uPlot.min.css";
function UPlot(props) {
  const plotRef = useRef();

  useEffect(() => {
    import("uplot").then((uPlot) => {
      new uPlot.default(props.options, props.data, plotRef.current);
    });
  }, [props]);

  return (
    <div>
      <div ref={plotRef} />
    </div>
  );
}

export default UPlot;

JannVazTor avatar Oct 28 '21 06:10 JannVazTor