React-Spring hooks: useSpringValue

Roza J
3 min readDec 18, 2022
Let’s bounce!

Follow through my chat GPT supported journey with learning react spring.

Today’s topic: useSpringValue

What’s the difference between `useSpring and `useSpringValue ?

useSpring is a hook in the react-spring library that allows you to create a spring that can be used to animate a single component or element. It takes an object of spring config values as an argument and returns an object containing the current animated values and a set of functions for controlling the spring.

useSpringValue is also a hook in the react-spring library that allows you to create a spring value that can be used to animate a single component or element. Like useSpring, useSpringValue takes an object of spring config values as an argument, but it returns a single animated value rather than an object containing the current animated values and control functions.

Here’s an example of how you might use useSpring and useSpringValue in a React component:

import { useSpring, useSpringValue } from 'react-spring'

function MyComponent() {
const [spring, setSpring] = useSpring({ from: { x: 0 }, to: { x: 100 } })
const springValue = useSpringValue({ from: { y: 0 }, to: { y: 100 } })

return (
<animated.div style={spring} onClick={() => setSpring({ x: 0 })}>
Spring
</animated.div>
<animated.div style={springValue}>Spring value</animated.div>
)
}

So… when to use `useSpring` and when to `useSpringValue`?

useSpring and useSpringValue are both useful for animating a single component or element in a React application using the react-spring library. The choice between the two largely depends on what you need to do with the spring in your component.

If you need to control the spring directly, for example by pausing or resetting it, or if you need to access the current animated values of the spring, useSpring is the better choice. useSpring returns an object containing the current animated values of the spring and a set of functions for controlling the spring, such as start, stop, and reset.

On the other hand, if you just need to use the spring to animate a single value and don’t need to control it directly, useSpringValue may be a simpler and more lightweight option. useSpringValue returns a single animated value that can be used to animate a component or element, but it doesn't provide any control functions or access to the current animated values.

Here’s an example of how you might use useSpring and useSpringValue in a React component:

import { useSpring, useSpringValue } from 'react-spring'

function MyComponent() {
const [spring, setSpring] = useSpring({ from: { x: 0 }, to: { x: 100 } })
const springValue = useSpringValue({ from: { y: 0 }, to: { y: 100 } })

return (
<>
<animated.div style={spring} onClick={() => setSpring({ x: 0 })}>
Spring
</animated.div>
<animated.div style={springValue}>Spring value</animated.div>
</>
)
}

In this example, useSpring is used to create a spring that is used to animate the x value of a single div element, and useSpringValue is used to create a single spring value that is used to animate the y value of another div element. The useSpring spring can be controlled directly using the setSpring function, while the useSpringValue spring value cannot be controlled directly.

--

--