Skip to content

Commit

Permalink
fix: useSpring do not useEffect to detect changes in tooltip x/y
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Jun 23, 2023
1 parent de262a7 commit 7f329e7
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/hooks/useSpring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function useSpring(
config: [number, number, number],
cb: (x: number) => void,
immediate?: boolean,
debug?: boolean
_debug?: boolean
) {
const springRef = React.useRef(new Spring(value, ...config))
const getValue = useGetLatest(value)
Expand All @@ -17,16 +17,22 @@ export function useSpring(
return springRef.current.done()
})

// Immediate
const prevRef = React.useRef<any>()
const changed = prevRef.current !== value

React.useEffect(() => {
if (immediate) {
springRef.current.snap(getValue())
if (changed) {
if (immediate) {
springRef.current.snap(getValue())
startRaf()
return
}
springRef.current.setEnd(value)
startRaf()
return
}
springRef.current.setEnd(value)
startRaf()
}, [debug, getValue, immediate, startRaf, stopRaf, value])

prevRef.current = value
})

React.useEffect(() => {
return () => {
Expand Down Expand Up @@ -54,3 +60,5 @@ export function useRaf(callback: () => any) {
),
]
}

// Test

0 comments on commit 7f329e7

Please sign in to comment.