Skip to content

Commit de1244b

Browse files
dagatsoinDaniel Neveux
andauthored
fix: spring does not start (#2372)
Co-authored-by: Daniel Neveux <[email protected]>
1 parent 195c479 commit de1244b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/core/src/hooks/useSprings.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ describe('useSprings', () => {
2626
}, isStrictMode)
2727

2828
describe('when only a props function is passed', () => {
29+
it('should reach final value in strict mode', async () => {
30+
update(1, () => ({
31+
from: { x: 0 },
32+
to: { x: 1 },
33+
}))
34+
expect(mapSprings(s => s.goal)).toEqual([{ x: 1 }])
35+
})
36+
2937
it('calls the props function once per new spring', () => {
3038
const getProps = jest.fn((i: number) => ({ x: i * 100 }))
3139

packages/core/src/hooks/useSprings.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,12 @@ export function useSprings(
127127
[]
128128
)
129129

130+
// useRef is needed to get the same references accross renders.
131+
// Note that controllers are a shallow copy of the final array.
132+
// So any array manipulation won't impact the final array until
133+
// the commit phase, in the useIsomorphicLayoutEffect callback
130134
const ctrls = useRef([...state.ctrls])
131135
const updates = useRef<any[]>([])
132-
updates.current ??= []
133136

134137
// Cache old controllers to dispose in the commit phase.
135138
const prevLength = usePrev(length) || 0
@@ -181,6 +184,10 @@ export function useSprings(
181184
const prevContext = usePrev(context)
182185
const hasContext = context !== prevContext && hasProps(context)
183186

187+
// This is the commit phase where the new transition will begin.
188+
// - updated animation values will be passed to the controlers.
189+
// - state will be updated
190+
// - springs will start or the new update will be queued if the spring is not started yet.
184191
useIsomorphicLayoutEffect(() => {
185192
layoutId.current++
186193

@@ -218,7 +225,6 @@ export function useSprings(
218225
ctrl.start(update)
219226
}
220227
}
221-
updates.current[i] = null
222228
})
223229
})
224230

0 commit comments

Comments
 (0)