Overview
MotionValue is a class used to track the state and velocity of motion values. It exists outside of React’s render cycle, making it efficient for high-frequency updates like animations and gestures.
Import
Constructor
Parameters
The initial value
Optional configuration:
owner: Internal owner reference (used by Motion components)
Methods
get()
Returns the latest state of theMotionValue.
set()
Sets the state of theMotionValue.
The new value to set
jump()
Set the state of theMotionValue, stopping any active animations, effects, and resetting velocity to 0.
The new value to set
Whether to stop active animations
on()
Subscribe toMotionValue events.
"change": Fired when the value changes"animationStart": Fired when an animation starts"animationComplete": Fired when an animation completes"animationCancel": Fired when an animation is cancelled"destroy": Fired when the MotionValue is destroyed
getPrevious()
Returns the previous state of theMotionValue.
getVelocity()
Returns the latest velocity of theMotionValue. Returns 0 if the state is non-numerical.
isAnimating()
Returnstrue if this value is currently animating.
stop()
Stop the currently active animation.destroy()
Destroy and clean up subscribers to thisMotionValue.
useMotionValue hook automatically handles cleanup. Only call destroy() if you manually created a MotionValue via the motionValue function.
Properties
updatedAt
The time theMotionValue was last updated (in milliseconds).
Usage
Creating Motion Values
In React:Subscribing to Changes
Listen for value updates:Animation Events
Track animation lifecycle:Manual Velocity Control
Set velocity explicitly:Stopping Animations
Jumping to Values
Instantly set a value without animation:Lifecycle
Automatic Cleanup (React)
When usinguseMotionValue, cleanup is automatic:
Manual Cleanup (Vanilla JS)
When creating values manually, clean up when done:Performance
- Motion values exist outside React’s render cycle
- Updates don’t trigger component re-renders
- Velocity is tracked automatically for numerical values
- Listeners are efficiently managed and cleaned up
Notes
- Motion values can hold any type of value (numbers, strings, colors, etc.)
- Velocity tracking only works for numerical values
- When all
"change"listeners are removed, active animations are automatically stopped - Motion values maintain a dependency graph for transformed values
- Maximum velocity delta is 30ms - older updates return velocity of
0
Related
- useMotionValue - React hook for creating motion values
- useTransform - Transform motion values
- useSpring - Add spring physics
- useVelocity - Track velocity
- animate - Animate motion values