-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLC.tsx
51 lines (45 loc) · 1.52 KB
/
LC.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// React logic for managing LightningChart JS instances that are shared
// between any LCJS based components that can be visible at the same time
// In simple use cases with 1-2 charts visible at once, there is no need to use these.
// However, with more charts visible at once, this gives an incredible performance advantage, since all charts use a shared LC context.
// See usage example in ./Components/MyChartComponent.js and ./App.js
import React, {
createContext,
useEffect,
useRef,
useState,
PropsWithChildren,
} from "react";
import { LightningChart, lightningChart } from "@lightningchart/lcjs";
export const LCContext = createContext<undefined | LightningChart>(undefined);
export function LCHost(props: PropsWithChildren) {
const lcRef = useRef<undefined | LightningChart>(undefined);
const [lcState, setLcState] = useState<undefined | LightningChart>(undefined);
useEffect(() => {
if (!lcRef.current) {
try {
lcRef.current = lightningChart({
license: process.env.REACT_APP_LCJS_LICENSE,
sharedContextOptions: {
useIndividualCanvas: true,
},
});
setLcState(lcRef.current);
} catch (e) {
console.error(e);
}
}
return () => {
if (lcRef.current && "dispose" in lcRef.current) {
lcRef.current.dispose();
lcRef.current = undefined;
setLcState(undefined);
}
};
}, []);
return (
<>
<LCContext.Provider value={lcState}>{props.children}</LCContext.Provider>
</>
);
}