1- import { createContext , ReactNode , useEffect , useState } from 'react' ;
1+ import { createContext , ReactNode , useMemo } from 'react' ;
22import { useTheme } from '@deephaven/components' ;
33import defaultChartTheme , { ChartTheme } from './ChartTheme' ;
44
@@ -20,20 +20,10 @@ export function ChartThemeProvider({
2020} : ChartThemeProviderProps ) : JSX . Element {
2121 const { activeThemes } = useTheme ( ) ;
2222
23- const [ chartTheme , setChartTheme ] = useState < ChartTheme | null > ( null ) ;
24-
25- // The `ThemeProvider` that supplies `activeThemes` also provides the corresponding
26- // CSS theme variables to the DOM by dynamically rendering <style> tags whenever
27- // the `activeThemes` change. Painting the latest CSS variables to the DOM may
28- // not happen until after `ChartThemeProvider` is rendered, but they should be
29- // available by the time the effect runs. Therefore, it is important to derive
30- // the chart theme in an effect instead of deriving in a `useMemo` to ensure
31- // we have the latest CSS variables.
32- useEffect ( ( ) => {
33- if ( activeThemes != null ) {
34- setChartTheme ( defaultChartTheme ( ) ) ;
35- }
36- } , [ activeThemes ] ) ;
23+ const chartTheme = useMemo (
24+ ( ) => ( activeThemes == null ? null : defaultChartTheme ( ) ) ,
25+ [ activeThemes ]
26+ ) ;
3727
3828 return (
3929 < ChartThemeContext . Provider value = { chartTheme } >
0 commit comments