uPlot
uPlot copied to clipboard
How to use uPlot with NextJS
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.
think you gotta wait until mount and init uPlot in a useEffect hook?
https://github.com/vercel/next.js/discussions/11027
@leeoniya Yes because we need to access uplot constructor
i'm not familiar with nextjs. does this solve your issue?
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;