File size: 931 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
import { useConstant, useOnce } from '@react-spring/shared'

import { SpringValue } from '../SpringValue'
import { SpringUpdate } from '../types'

/**
 * Creates a constant single `SpringValue` that can be interacted
 * with imperatively. This is an advanced API and does not react
 * to updates from the parent component e.g. passing a new initial value
 *
 *
 * ```jsx
 * export const MyComponent = () => {
 *   const opacity = useSpringValue(1)
 *
 *   return <animated.div style={{ opacity }} />
 * }
 * ```
 *
 * @param initial – The initial value of the `SpringValue`.
 * @param props – Typically the same props as `useSpring` e.g. `config`, `loop` etc.
 *
 * @public
 */
export const useSpringValue = <T>(
  initial: Exclude<T, object>,
  props?: SpringUpdate<T>
) => {
  const springValue = useConstant(() => new SpringValue(initial, props))

  useOnce(() => () => {
    springValue.stop()
  })

  return springValue
}