docs/app/routes/docs.advanced.events.mdx
import { formatFrontmatterToRemixMeta } from '../helpers/meta'
export const meta = formatFrontmatterToRemixMeta(frontmatter)
There are a handful of events you can use to react to the state of animation at certain points in time.
Every event function can either be a function on it's own or be an object to which it's keys correlate to the keys of the spring:
useSpring(
() => ({
x: 0,
y: 0,
// onStart is called when the animation of the spring starts
onStart: () => console.log('the spring has started'),
}),
[]
)
useSpring(
() => ({
x: 0,
y: 0,
onStart: {
// onStart is called for each key when the animation starts
x: () => console.log('x key has started'),
y: () => console.log('y key has started'),
},
}),
[]
)
The latter form can be useful if for example you have different configs for different keys. Or you want to do different events based on different keys, e.g. you want to imperatively update an objects rotation based on the x key.
Called when the animation begins.
:::warning
onStart is called after the first animation tick, this value is therefore considered dirty.
:::
type OnStart = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
Called on every frame.
type OnChange = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
Called when the animation comes to a stand-still.
type OnRest = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
Called when the animation is paused.
type OnPause = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
Called when the animation is resumed.
type OnResume = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
Called when the promise for the update is resolved.
type OnResolve = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
Called after an animation is updated by new props, even if the animation remains idle.
type OnProps = (
props: {[key: string]: any}
spring: SpringValue,
) => void
Called after a transition item is unmounted.
type OnDestroyed = (
item: Item
key: string | number
) => void
interface AnimationResult {
// Type inference will solve this for you.
value: SpringValue | { [keyof SpringValues]: number} | number
finished: boolean
cancelled: boolean
}
ItemThe Item argument is only present when using the relevant events within the useTransition hook.