Skip to content

Commit 986bc54

Browse files
refactor: correct dependencies to ensure render once
1 parent 086256e commit 986bc54

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

charts/core/src/Series/Line/Line.tsx

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
22
import { useSeriesContext } from '@lg-charts/series-provider';
3-
import _ from 'lodash';
43

54
import { useDarkMode } from '@leafygreen-ui/leafygreen-provider';
65

@@ -11,40 +10,39 @@ import { SeriesProps } from '../Series.types';
1110

1211
export type LineProps = SeriesProps;
1312

14-
const defaultLineOptions: EChartLineSeriesOption = {
15-
type: 'line',
16-
showSymbol: false,
17-
symbol: 'circle',
18-
clip: false,
19-
symbolSize: 7,
20-
emphasis: {
21-
focus: 'series',
22-
},
23-
blur: {
24-
lineStyle: {
25-
opacity: 0.5,
13+
function getDefaultLineOptions(seriesColor: string): EChartLineSeriesOption {
14+
return {
15+
type: 'line',
16+
showSymbol: false,
17+
symbol: 'circle',
18+
clip: false,
19+
symbolSize: 7,
20+
emphasis: {
21+
focus: 'series',
22+
},
23+
blur: {
24+
lineStyle: {
25+
opacity: 0.5,
26+
},
27+
},
28+
itemStyle: {
29+
color: seriesColor || undefined
2630
},
27-
},
28-
lineStyle: {
29-
width: 1,
30-
},
31-
};
31+
lineStyle: {
32+
color: seriesColor || undefined,
33+
width: 1,
34+
}
35+
}
36+
}
3237

3338
export function Line(props: LineProps) {
3439
const { getColor } = useSeriesContext();
3540
const { theme } = useDarkMode();
36-
const color = getColor(props.name, theme);
37-
const colorOverrides: EChartLineSeriesOption = {
38-
lineStyle: { color: color || undefined },
39-
itemStyle: { color: color || undefined },
40-
};
41-
42-
const options: EChartSeriesOption = _.merge(
43-
{},
44-
defaultLineOptions,
45-
props,
46-
colorOverrides,
47-
);
41+
42+
const options: EChartSeriesOption = useMemo(() => ({
43+
...getDefaultLineOptions(getColor(props.name, theme)),
44+
...props
45+
}), [props, getColor, theme]);
4846

4947
return <Series {...options} />;
5048
}

charts/core/src/Series/Series.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function Series(options: EChartSeriesOption) {
2828
*/
2929
removeSeries(options.name);
3030
};
31-
}, [addSeries, isVisible, ready, removeSeries]);
31+
}, [addSeries, isVisible, ready, removeSeries, options]);
3232

3333
return null;
3434
}

0 commit comments

Comments
 (0)