File size: 6,557 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
---
meta:
  title: Getting started | React Spring
  'og:title': Getting started | React Spring
  'twitter:title': Getting started | React Spring
  description: Get started with react-spring by following this step by step guide to get up and running.
  'og:description': Get started with react-spring by following this step by step guide to get up and running.
  'twitter:description': Get started with react-spring by following this step by step guide to get up and running.
  'og:url': https://www.react-spring.dev/docs/getting-started
  'twitter:url': https://www.react-spring.dev/docs/getting-started
sidebar_position: 2
---

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

export const meta = formatFrontmatterToRemixMeta(frontmatter)

# Getting started

React Spring is a library for building interactive, data-driven, and animated UI components. It can animate HTML, SVG, Native Elements, Three.js, and more.

By the end of this quick guide, you'll have installed React Spring and created your first web-based animation! This animation will see a normal div move across the screen.

## Install

React Spring can be installed with any package manager. Here's how to install it with [Yarn](https://yarnpkg.com/en/):

### For react >= 19

```jsx copy="yarn add @react-spring/web"
yarn add @react-spring/web
```

### For react < 19

```jsx copy="yarn add @react-spring/web@9"
yarn add @react-spring/web@9
```

## The Animated Element

The actual component that handles animation is our `animated` component. This is just a higher-order
component (HOC) if you're familiar with that pattern. If you're not see this explanation from the
`react.js` docs:

> a higher-order component is a function that takes a component and returns a new component.

So really, it's just a fancy wrapper. To use it, we need to import it:

```jsx copy="import { animated } from '@react-spring/web'"
import { animated } from '@react-spring/web'
```

We use our animated component like any other JSX element and to ensure we can see it, we'll add some styling:

```jsx defaultOpen=true line=5
import { animated } from '@react-spring/web'

export default function MyComponent() {
  return (
    <animated.div
      style={{
        width: 80,
        height: 80,
        background: '#ff6d6d',
        borderRadius: 8,
      }}
    />
  )
}
```

Now we're ready to add our hook & animate the component!

## The Hook

Meet your first hook, our signature hook really – `useSpring`, first we need to import it:

```jsx copy="import { useSpring, animated } from '@react-spring/web'"
import { useSpring, animated } from '@react-spring/web'
```

To use `useSpring`, we treat it like any other hook:

```jsx line=4-8
import { useSpring, animated } from '@react-spring/web'

export default function MyComponent() {
  const springs = useSpring({
    from: { x: 0 },
    to: { x: 100 },
  })

  return (
    <animated.div
      style={{
        width: 80,
        height: 80,
        background: '#ff6d6d',
        borderRadius: 8,
      }}
    />
  )
}
```

We use the keywords `from` and `to` to define the start and end values of our animation.
So in this instance, we're starting with an `x` value of 0 and ending with a value of 100.

## Your First Animation

`useSpring` doesn't actually animate anything though. It just returns `SpringValues` that we pass to
our animated component. So that when the springs are applied and the component is mounted it will
move to the right. These springs are passed to the animated component like so:

```jsx live=true defaultOpen=true line=10
import { useSpring, animated } from '@react-spring/web'

export default function MyComponent() {
  const springs = useSpring({
    from: { x: 0 },
    to: { x: 100 },
  })

  return (
    <animated.div
      style={{
        width: 80,
        height: 80,
        background: '#ff6d6d',
        borderRadius: 8,
        ...springs,
      }}
    />
  )
}
```

And there we have it! Your first animated component.

## Reacting to events

Very rarely do you find yourself only needing an animation to occur only on mount,
we normally want animations to occur on a user interaction. Whether that's
`mouseenter`, `click`, `keydown` or any event that could occur. So how do we do
this very common use-case?

`useSpring` can take two types of first argument, a `config` object and a `function`.
We're going to explore the latter in more detail, we'll start by changing the notation of our hook.

```jsx line=4-6
import { useSpring, animated } from '@react-spring/web'

export default function MyComponent() {
  const [springs, api] = useSpring(() => ({
    from: { x: 0 },
  }))

  return (
    <animated.div
      style={{
        width: 80,
        height: 80,
        background: '#ff6d6d',
        borderRadius: 8,
        ...springs,
      }}
    />
  )
}
```

When we provide a function to `useSpring` we get an array returned, with the first
argument as our `springs` which we're already used to (as this is returned when you
provide only a config object) and the second argument is the `api` that controls
these springs.

We'll start with a very basic user interaction, the `onClick` event by creating a
handler and in that handler we'll use the `api.start` method. The `start` method starts
our animation with the configuration we provide to it, like so:

```jsx live=true defaultOpen=true line=8-18
import { useSpring, animated } from '@react-spring/web'

export default function MyComponent() {
  const [springs, api] = useSpring(() => ({
    from: { x: 0 },
  }))

  const handleClick = () => {
    api.start({
      from: {
        x: 0,
      },
      to: {
        x: 100,
      },
    })
  }

  return (
    <animated.div
      onClick={handleClick}
      style={{
        width: 80,
        height: 80,
        background: '#ff6d6d',
        borderRadius: 8,
        ...springs,
      }}
    />
  )
}
```

The `api` value has many different methods that we can use to control our animation.
We can `stop` our animations, we can `set` them (to change the value without animation)
and much more.

## Next Steps

Whilst this was a brief introduction to `react-spring`, through this tutorial you've
learnt about these three key areas:

- the `animated` component and how to use it with HTML elements
- the `useSpring` hook with either a configuration object or with a function
- how to use the `api` object to control your animation and react to events

From here, you could learn how to use our other hooks or more about the configuration
objects we pass to the animation hooks.