Skip to content

Commit 4100ff3

Browse files
committed
make useChartInteractionState react compiler compliant
1 parent fdc19a0 commit 4100ff3

2 files changed

Lines changed: 25 additions & 29 deletions

File tree

src/components/Charts/hooks/useChartInteractionState.ts

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useMemo, useState} from 'react';
1+
import {useState} from 'react';
22
import type {SharedValue} from 'react-native-reanimated';
33
import {makeMutable, useAnimatedReaction} from 'react-native-reanimated';
44
import {scheduleOnRN} from 'react-native-worklets';
@@ -96,34 +96,29 @@ function useChartInteractionState<Init extends ChartInteractionStateInit>(
9696
state: ChartInteractionState<Init>;
9797
isActive: boolean;
9898
} {
99-
const keys = Object.keys(initialValues.y).join(',');
99+
const yState = {} as Record<keyof Init['y'], {value: SharedValue<number>; position: SharedValue<number>}>;
100100

101-
const state = useMemo(() => {
102-
const yState = {} as Record<keyof Init['y'], {value: SharedValue<number>; position: SharedValue<number>}>;
103-
104-
for (const [key, initVal] of Object.entries(initialValues.y)) {
105-
yState[key as keyof Init['y']] = {
106-
value: makeMutable(initVal),
107-
position: makeMutable(0),
108-
};
109-
}
110-
111-
return {
112-
isActive: makeMutable(false),
113-
matchedIndex: makeMutable(-1),
114-
x: {
115-
value: makeMutable(initialValues.x),
116-
position: makeMutable(0),
117-
},
118-
y: yState,
119-
yIndex: makeMutable(-1),
120-
cursor: {
121-
x: makeMutable(0),
122-
y: makeMutable(0),
123-
},
101+
for (const [key, initVal] of Object.entries(initialValues.y)) {
102+
yState[key as keyof Init['y']] = {
103+
value: makeMutable(initVal),
104+
position: makeMutable(0),
124105
};
125-
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps -- keys is a stable string representation of y keys
126-
}, [keys]);
106+
}
107+
108+
const state: ChartInteractionState<Init> = {
109+
isActive: makeMutable(false),
110+
matchedIndex: makeMutable(-1),
111+
x: {
112+
value: makeMutable(initialValues.x),
113+
position: makeMutable(0),
114+
},
115+
y: yState,
116+
yIndex: makeMutable(-1),
117+
cursor: {
118+
x: makeMutable(0),
119+
y: makeMutable(0),
120+
},
121+
};
127122

128123
const isActive = useIsInteractionActive(state);
129124

src/components/Charts/hooks/useChartInteractions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {scheduleOnRN} from 'react-native-worklets';
66
import {TOOLTIP_BAR_GAP} from '@components/Charts/constants';
77
import {useChartInteractionState} from './useChartInteractionState';
88

9+
const INITIAL_INTERACTION_STATE = {x: 0, y: {y: 0}};
10+
911
/**
1012
* Arguments passed to the checkIsOver callback for hit-testing
1113
*/
@@ -76,8 +78,7 @@ type CartesianActionsHandle = {
7678
*/
7779
function useChartInteractions({handlePress, checkIsOver, barGeometry}: UseChartInteractionsProps) {
7880
/** Interaction state compatible with Victory Native's internal logic */
79-
const {state: chartInteractionState, isActive: isTooltipActiveState} = useChartInteractionState({x: 0, y: {y: 0}});
80-
81+
const {state: chartInteractionState, isActive: isTooltipActiveState} = useChartInteractionState(INITIAL_INTERACTION_STATE);
8182
/** Ref passed to CartesianChart to allow manual touch injection */
8283
const actionsRef = useRef<CartesianActionsHandle>(null);
8384

0 commit comments

Comments
 (0)