The SpringRef is React Spring's imperative API, allowing you to control animations outside of the standard declarative React render cycle.
For functional components, the recommended way to initialize it is via the useSpringRef hook. You then pass this ref to a useSpring call to link the imperative controller to the spring animation.
For class components or environments where hooks are unavailable, you can initialize it by calling the SpringRef() function directly.
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>
}