File size: 4,159 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
---
meta:
  title: SpringRef | React Spring
  'og:title': SpringRef | React Spring
  'twitter:title': SpringRef | React Spring
  description: An advanced API guide to the SpringRef function in React Spring.
  'og:description': An advanced API guide to the SpringRef function in React Spring.
  'twitter:description': An advanced API guide to the SpringRef function in React Spring.
  'og:url': https://www.react-spring.dev/docs/advanced/spring-ref
  'twitter:url': https://www.react-spring.dev/docs/advanced/spring-ref
sidebar_position: 5
---

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

export const meta = formatFrontmatterToRemixMeta(frontmatter)

# SpringRef

Our imperative API. You can initialise it via function call, or for better practice use the hook `useSpringRef`.
For more contextual information for this function and why it's so important, see [here](/docs/concepts/imperative-api).

## Usage

### Hook

```ts
import { animated, useSpring, useSpringRef } from '@react-spring/web'

function MyComponent() {
  const api = useSpringRef()

  const props = useSpring({
    ref: api,
    from: { opacity: 0 },
    to: { opacity: 1 },
  })

  return <animated.div style={props}>Hello World</animated.div>
}
```

### Function call

```ts
import { Component, createRef } from 'react'
import { Controller, animated, SpringRef } from '@react-spring/web'

class AnimatedComponent extends Component {
  isShowing = createRef(false)
  api = SpringRef()
  animations = new Controller({ opacity: 0, ref: this.api })

  toggle = () => {
    this.animations.start({ opacity: this.isShowing ? 1 : 0 })

    this.isShowing = !this.isShowing
  }

  render() {
    return (
      <>
        <button onClick={this.toggle}>click</button>
        <animated.div style={this.animations.springs}>I will fade</animated.div>
      </>
    )
  }
}
```

## Properties

import { TablesConfiguration } from '../components/Tables/TablesConfig'

<TablesConfiguration data={[['current', 'Controller[]', '[]']]} />

## Methods

### Add

Add a controller to this ref.

```ts
add(ctrl: Controller<State>): void
```

### Delete

Remove a controller from this ref.

```ts
delete(ctrl: Controller<State>): void
```

### Pause

Pause some or all animations by passing `SpringValue` keys.

```ts
pause(): this
pause(keys: OneOrMore<string>): this
pause(keys?: OneOrMore<string>): this
```

### Resume

Resume some or all animations by passing `SpringValue` keys.

```ts
resume(): this
resume(keys: OneOrMore<string>): this
resume(keys?: OneOrMore<string>): this
```

### Set

Update the state of each controller without animating. Accepts either a partial state object or a
function that returns a partial state object.

```ts
set(values: Partial<State>): void
set(values: (index: number, ctrl: Controller<State>) => Partial<State>): void
```

### Start

Start the queued animations of each controller. Alternatively pass a `ControllerUpdate` object to
update the state of each controller before starting the animations. Or pass a function to edit the
`ControllerUpdate` depending on the specific `Controller`.

```ts
start(): AsyncResult<Controller<State>>[]
start(props: ControllerUpdate<State>): AsyncResult<Controller<State>>[]
start(props: ControllerUpdateFn<State>): AsyncResult<Controller<State>>[]
start(
props?: ControllerUpdate<State> | ControllerUpdateFn<State>
): AsyncResult<Controller<State>>[]
```

### Stop

Stop some or all of the animations by passing `SpringValue` keys. Additionall, cancel those animations
by passing a `boolean` as the first argument and the `keys` as the second.

```ts
stop(): this
stop(keys: OneOrMore<string>): this
stop(cancel: boolean): this
stop(cancel: boolean, keys: OneOrMore<string>): this
stop(keys?: OneOrMore<string>): this
stop(cancel: boolean, keys?: OneOrMore<string>): this
```

### Update

Add the same props to each controller's update queue. Or alternatively generate separate props for each
controller's update queue with a function.

```ts
update(props: ControllerUpdate<State>): this
update(props: ControllerUpdateFn<State>): this
update(props: ControllerUpdate<State> | ControllerUpdateFn<State>): this
```