Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/core/src/hooks/useSprings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ describe('useSprings', () => {
}, isStrictMode)

describe('when only a props function is passed', () => {
it('should reach final value in strict mode', async () => {
update(1, () => ({
from: { x: 0 },
to: { x: 1 },
}))
expect(mapSprings(s => s.goal)).toEqual([{ x: 1 }])
})

it('calls the props function once per new spring', () => {
const getProps = jest.fn((i: number) => ({ x: i * 100 }))

Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/hooks/useSprings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
// Create a local ref if a props function or deps array is ever passed.
const ref = useMemo(
() => (propsFn || arguments.length == 3 ? SpringRef() : void 0),
[]

Check warning on line 85 in packages/core/src/hooks/useSprings.ts

View workflow job for this annotation

GitHub Actions / Style Checks

React Hook useMemo has a missing dependency: 'propsFn'. Either include it or remove the dependency array
)

interface State {
Expand Down Expand Up @@ -124,12 +124,15 @@
})
},
}),
[]

Check warning on line 127 in packages/core/src/hooks/useSprings.ts

View workflow job for this annotation

GitHub Actions / Style Checks

React Hook useMemo has a missing dependency: 'forceUpdate'. Either include it or remove the dependency array
)

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

// Cache old controllers to dispose in the commit phase.
const prevLength = usePrev(length) || 0
Expand All @@ -145,13 +148,13 @@
ctrls.current.length = length

declareUpdates(prevLength, length)
}, [length])

Check warning on line 151 in packages/core/src/hooks/useSprings.ts

View workflow job for this annotation

GitHub Actions / Style Checks

React Hook useMemo has missing dependencies: 'declareUpdates', 'prevLength', and 'ref'. Either include them or remove the dependency array

// Update existing controllers when "deps" are changed.
useMemo(() => {
declareUpdates(0, Math.min(prevLength, length))
// @ts-expect-error – we want to allow passing undefined to useMemo
}, deps)

Check warning on line 157 in packages/core/src/hooks/useSprings.ts

View workflow job for this annotation

GitHub Actions / Style Checks

React Hook useMemo has missing dependencies: 'declareUpdates', 'length', and 'prevLength'. Either include them or remove the dependency array

Check warning on line 157 in packages/core/src/hooks/useSprings.ts

View workflow job for this annotation

GitHub Actions / Style Checks

React Hook useMemo was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies

/** Fill the `updates` array with declarative updates for the given index range. */
function declareUpdates(startIndex: number, endIndex: number) {
Expand Down Expand Up @@ -181,6 +184,10 @@
const prevContext = usePrev(context)
const hasContext = context !== prevContext && hasProps(context)

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

Expand Down Expand Up @@ -218,7 +225,6 @@
ctrl.start(update)
}
}
updates.current[i] = null
})
})

Expand Down
Loading