File size: 3,513 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
---
meta:
title: Events | React Spring
'og:title': Events | React Spring
'twitter:title': Events | React Spring
description: An advanced guide to the event system of Controllers, Components & SpringValues in React Spring.
'og:description': An advanced guide to the event system of Controllers, Components & SpringValues in React Spring.
'twitter:description': An advanced guide to the event system of Controllers, Components & SpringValues in React Spring.
'og:url': https://www.react-spring.dev/docs/advanced/events
'twitter:url': https://www.react-spring.dev/docs/advanced/events
sidebar_position: 2
---
import { formatFrontmatterToRemixMeta } from '../helpers/meta'
export const meta = formatFrontmatterToRemixMeta(frontmatter)
# Events
There are a handful of events you can use to react to the state of animation at certain points in time.
## Keys vs Springs
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:
```jsx line=5-6,15-19
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.
## onStart
Called when the animation begins.
:::warning
`onStart` is called after the first animation tick, this value is therefore considered dirty.
:::
```ts
type OnStart = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
```
## onChange
Called on every frame.
```ts
type OnChange = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
```
## onRest
Called when the animation comes to a stand-still.
```ts
type OnRest = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
```
## onPause
Called when the animation is paused.
```ts
type OnPause = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
```
## onResume
Called when the animation is resumed.
```ts
type OnResume = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
```
## onResolve
Called when the promise for the update is resolved.
```ts
type OnResolve = (
result: AnimationResult,
spring: Controller | SpringValue,
item?: Item
) => void
```
## onProps
Called after an animation is updated by new props, even if the animation remains idle.
```ts
type OnProps = (
props: {[key: string]: any}
spring: SpringValue,
) => void
```
## onDestroyed
Called after a transition item is unmounted.
```ts
type OnDestroyed = (
item: Item
key: string | number
) => void
```
## Typescript
```ts
interface AnimationResult {
// Type inference will solve this for you.
value: SpringValue | { [keyof SpringValues]: number} | number
finished: boolean
cancelled: boolean
}
```
### A note on `Item`
The `Item` argument is only present when using the relevant events within the [`useTransition`](/docs/components/use-transition) hook.
|