File size: 3,839 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
---
meta:
  title: useTransition | React Spring
  'og:title': useTransition | React Spring
  'twitter:title': useTransition | React Spring
  description: API documentation for the useTransition hook in React Spring.
  'og:description': API documentation for the useTransition hook in React Spring.
  'twitter:description': API documentation for the useTransition hook in React Spring.
  'og:url': https://www.react-spring.dev/docs/components/use-transition
  'twitter:url': https://www.react-spring.dev/docs/components/use-transition
sidebar_position: 4
---

import { formatFrontmatterToRemixMeta } from '../helpers/meta'

export const meta = formatFrontmatterToRemixMeta(frontmatter)

# useTransition

This hook is best suited for animating in & out datasets or items you don't particularly
want to be left in the DOM, e.g. a dialog.

## Usage

`useTransition` depends on an `array` of data. That data can be anything you want, we
use a lot of internals to track each datum including inferring the keys, this is the
first argument. The second is a config object, which is different to
[`useSpring`](/docs/components/use-spring) or [`useSprings`](/docs/components/use-springs) so take note!

### With a function & deps

```jsx lines=1,4-8,10-12
import { useTransition, animated } from '@react-spring/web'

function MyComponent({ data = [1, 2, 3] }) {
  const [transitions, api] = useTransition(data, () => ({
    from: { opacity: 0 },
    enter: { opacity: 1 },
    leave: { opacity: 1 },
  }))

  return transitions((style, item) => (
    <animated.div style={style}>{item}</animated.div>
  ))
}
```

### With a config object

```jsx lines=1,4-8,10-12
import { useTransition, animated } from '@react-spring/web'

function MyComponent({ data = [1, 2, 3] }) {
  const transitions = useTransition(data, {
    from: { opacity: 0 },
    enter: { opacity: 1 },
    leave: { opacity: 1 },
  })

  return transitions((style, item) => (
    <animated.div style={style}>{item}</animated.div>
  ))
}
```

### Transition function

The transition function takes a `render` function as an argument. This is how we append `keys`.
From the example above you can see we pass a `style` argument to the function, this `style` object
relates to the state of the animation, e.g. if the animation is `ENTERING` then the we use the
keys from the `enter` property of of your config object. For a deeper dive into the function
signature see the [`Typescript`](#typescript) section.

## Reference

`Item` is defined a lot below, it's automatically inferred from what you pass as the
content of the array you pass as the first argument to the hook. Therefore, if you
passed `[1, 2, 3]` then `Item` would be `number`.

import { TablesConfiguration } from '../components/Tables/TablesConfig'
import { TRANSITION_CONFIG_DATA } from '../data/fixtures'

<TablesConfiguration data={TRANSITION_CONFIG_DATA} />

## Typescript

```tsx
function useTransition<Item extends any>(
  data: Item[],
  configuration: ConfigObject
): TransitionFn<Item>

function useTransition<Item extends any>(
  data: Item[],
  configurationFn: () => ConfigObject
  deps?: any[]
): [transition: TransitionFn<Item>, api: SpringRef]

type TransitionFn = (
  style: SpringValues,
  item: Item,
  transitionState: TransitionState<Item>,
  index: number
) => ReactNode
```

Where `ConfigObject` is described [above](#reference)

### TS Glossary

- [`TransitionState`](/docs/typescript#transitionstate)

## Examples

import { ExampleGrid } from '../components/Grids/ExampleGrid'

<ExampleGrid
  sandboxTitles={[
    'Basic Transition',
    'Chaining Transition and a Spring',
    'Exit Before Enter',
    'Image Fade',
    'List Reordering',
    'Masonry Grid',
    'Multistage Transitions',
    'Notification Hub',
  ]}
/>

Can't find what you're looking for? Check out all our [examples!](/examples)