Skip to content

Commit 48666ad

Browse files
[charts] Only update store if interaction item is different (#17851)
1 parent 82ed063 commit 48666ad

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

packages/x-charts/src/internals/plugins/featurePlugins/useChartHighlight/useChartHighlight.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useAssertModelConsistency } from '@mui/x-internals/useAssertModelConsistency';
22
import useEventCallback from '@mui/utils/useEventCallback';
33
import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
4+
import { fastObjectShallowCompare } from '@mui/x-internals/fastObjectShallowCompare';
45
import { ChartPlugin } from '../../models';
56
import { HighlightItemData, UseChartHighlightSignature } from './useChartHighlight.types';
67

@@ -33,6 +34,12 @@ export const useChartHighlight: ChartPlugin<UseChartHighlightSignature> = ({ sto
3334
});
3435

3536
const setHighlight = useEventCallback((newItem: HighlightItemData) => {
37+
const prevItem = store.getSnapshot().highlight.item;
38+
39+
if (fastObjectShallowCompare(prevItem, newItem)) {
40+
return;
41+
}
42+
3643
params.onHighlightChange?.(newItem);
3744
store.update((prev) => ({ ...prev, highlight: { item: newItem } }));
3845
});

packages/x-charts/src/internals/plugins/featurePlugins/useChartInteraction/useChartInteraction.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import useEventCallback from '@mui/utils/useEventCallback';
2+
import { fastObjectShallowCompare } from '@mui/x-internals/fastObjectShallowCompare';
23
import { ChartPlugin } from '../../models';
34
import { Coordinate, UseChartInteractionSignature } from './useChartInteraction.types';
45
import { ChartItemIdentifier, ChartSeriesType } from '../../../../models/seriesType/config';
@@ -55,13 +56,19 @@ export const useChartInteraction: ChartPlugin<UseChartInteractionSignature> = ({
5556
);
5657

5758
const setItemInteraction = useEventCallback((newItem: ChartItemIdentifier<ChartSeriesType>) => {
58-
store.update((prev) => ({
59-
...prev,
60-
interaction: {
61-
...prev.interaction,
62-
item: newItem,
63-
},
64-
}));
59+
store.update((prev) => {
60+
if (fastObjectShallowCompare(prev.interaction.item, newItem)) {
61+
return prev;
62+
}
63+
64+
return {
65+
...prev,
66+
interaction: {
67+
...prev.interaction,
68+
item: newItem,
69+
},
70+
};
71+
});
6572
});
6673

6774
const setPointerCoordinate = useEventCallback((coordinate: Coordinate | null) => {

0 commit comments

Comments
 (0)