Skip to content

Commit 7f329e7

Browse files
committed
fix: useSpring do not useEffect to detect changes in tooltip x/y
1 parent de262a7 commit 7f329e7

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/hooks/useSpring.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function useSpring(
77
config: [number, number, number],
88
cb: (x: number) => void,
99
immediate?: boolean,
10-
debug?: boolean
10+
_debug?: boolean
1111
) {
1212
const springRef = React.useRef(new Spring(value, ...config))
1313
const getValue = useGetLatest(value)
@@ -17,16 +17,22 @@ export function useSpring(
1717
return springRef.current.done()
1818
})
1919

20-
// Immediate
20+
const prevRef = React.useRef<any>()
21+
const changed = prevRef.current !== value
22+
2123
React.useEffect(() => {
22-
if (immediate) {
23-
springRef.current.snap(getValue())
24+
if (changed) {
25+
if (immediate) {
26+
springRef.current.snap(getValue())
27+
startRaf()
28+
return
29+
}
30+
springRef.current.setEnd(value)
2431
startRaf()
25-
return
2632
}
27-
springRef.current.setEnd(value)
28-
startRaf()
29-
}, [debug, getValue, immediate, startRaf, stopRaf, value])
33+
34+
prevRef.current = value
35+
})
3036

3137
React.useEffect(() => {
3238
return () => {
@@ -54,3 +60,5 @@ export function useRaf(callback: () => any) {
5460
),
5561
]
5662
}
63+
64+
// Test

0 commit comments

Comments
 (0)