React Spring Library
React Spring is an animation library for React that is based on spring physics. Rather than using CSS animations or frame-by-frame animations, React Spring uses spring physics and simulations for real world behavior to make animations more fluid and realistic.
React Spring works for both web and React Native and comes with composable hooks that can seamlessly mesh with your components.
Insallation
npm install @react-spring/web
Hooks
It has several hooks depends on the animation need we can use.
- useSpring
- useSprings
- useTransition
- useTrail
- useChain
useSpring
Transforms one or more values from a start state to an end state. Good for simple UI fades, sliding, scaling, etc. Returns a style object to apply to animated components.
useSprings
For a list of items creates multiple springs for animating. Each item gets its own animation config and state. Ideal for use on animated dynamic lists or grids.
useTransition
For items animation to mount, update, or leave the DOM. Handles smooth transitions for the enter/leave states. Best for Modals, toasts or dynamic list filtering.
useTrail
Implements a follow the leader animation effect on multiple items. Each of the items from the list start animation after the one before them. Best for staggered reveals or stepwise transitions.
useChain
Runs multiple animations in sequence. For complex, timed transitions, this would be useful. The delay, and order between animations can be controlled.
Dynamically Animate Width With React Spring and react-use-measure
Integrated web animations harness user attention and improve their web experience. React Spring is one of many libraries that simplifies smooth transitions in React. In this article, we’ll run through a small interactive example that:
- Acts a a fill bar whose width changes in accordance to fill.
- Modifies and displays the width value in real time.
- Uses both @react-spring/web and react-use-measure.
- The required React and useMeasure, useState, and useSpring libraries are imported to manage the component state, measure the DOM, and handle animations seamlessly.
- The toggle state that was created through useState, determines when the width animation is to be expanded or collapsed.
- With the useMeasure hook, the container’s width is measured. It gives a reference to the DOM element and also yields the measured width.
- The animation is set up using useSpring. It is configured to animate from 0 to the container’s width when expanded and back to 0 when collapsed.
- Through a the container is implemented that is set to the measured width, hence useMeasure is able to capture the value.
- When the container is clicked, it changes the open state which determines when the animation is expanded or collapsed.
- An animated acts as a fill bar. It uses the fill value to increase and decrease its width visually within the container.
- Another animated showcases the live width value in motion as the animation progresses.

Output:
Initially output should be like this when click inside the box width value in motion as the animation progresses.


Key Benefits Using React Spring
1. Physics-Based Animations
React Spring uses spring physics instead of time-based animations like CSS transitions. Animations are more adaptive and more fluid when responding to the environment instead of fixed timing windows.
2. Declarative API
You just need to describe the animation’s final state, and the library will take care of the rest. This method works well in conjunction with React component model.
3. Performance Optimized
Works with request Animation Frame and does not cause layout thrashing. Even on more affordable devices, complex UI animations are no challenge.
4. Supports Interruptible Animations
Stop, reverse, or change the heading of animations mid-progress and they will still remain smooth. This is beneficial for user-controlled animations where the user can change what the animation is targeting.
5. Works with Both React DOM and React Native
One library serves both mobile and web, lessening the burden of learning multiple APIs.
6. Composable and Flexible
Animations are no longer restricted to static elements, but can include numbers, colors, paths, 3D rotations, and scrolls. One animation can be synchronized with other animations, and several animations can be combined to build one complex animation.