rive-react
repository·main·Indexed 22 days ago
https://github.com/rive-app/rive-reactA React wrapper for the rive-js/Wasm runtime that provides components and hooks for rendering interactive, stateful animations. It includes the Rive component for rendering assets, hooks for state machine and ViewModel control (such as useRive and useViewModelInstance), and specialized packages like @rive-app/react-canvas and @rive-app/react-canvas-lite for different bundle size and feature requirements. Compatible with React versions ^16.8.0 through ^19.0.0.
What's inside rive-react
- Rive React is a React wrapper around the Rive JS/Wasm runtime. It provides React components and hooks to integrate Rive's interactive, stateful graphics and vector animations into React applications. It allows developers to bridge the gap between the Rive Editor (where animations are designed) and web applications, ensuring high-fidelity playback of state machines, layout, and interactive elements.
Use @rive-app/react-canvas for Rive animations in React
mainThe@rive-app/react-canvaspackage provides a React wrapper for Rive animations, utilizing the@rive-app/canvasJavaScript runtime. It is designed to integrate Rive's high-performance canvas rendering directly into React applications.Use @rive-app/react-canvas-lite for smaller package sizes
mainThe
@rive-app/react-canvas-litepackage is a lightweight version of the standard@rive-app/react-canvaspackage. It uses the@rive-app/canvas-liteJS runtime and shares the same API as the standard version.Use this version when you need to minimize your application's bundle size. However, be aware of the following functional limitations:
- No Rive Text rendering: Rive Text components will not be rendered on the canvas.
- No Rive Audio: Rive Audio will not play.
Note that even if your Rive file contains Text or Audio components, using the Lite version will not cause application errors or prevent the rest of the graphic from rendering.
Getting started with Rive React
mainTo integrate Rive React into your application, follow the official quickstart guides and API documentation provided by Rive:
- Quickstart Guide: Getting Started with Rive in React
- API Reference: API documentation
For specific runtime features, refer to the following documentation topics:
- Animation Playback: Controlling how animations play.
- Layout: Managing how Rive assets are positioned and sized.
- State Machines: Interacting with Rive's state machine logic.
- Rive Text: Working with text elements within animations.
- Rive Events: Handling events emitted from the Rive runtime.
- Loading Assets: Managing how external assets are loaded.
Use Rive hooks for state and control
mainThe package provides several hooks to interact with the Rive runtime instance, manage state machines, and manipulate ViewModels.
Key hooks include:
useRive: Provides access to the Rive instance.useStateMachineInput: Allows you to control specific inputs within a state machine.useRiveFile: Manages loading Rive files.useResizeCanvas: Handles canvas resizing logic.
ViewModel hooks allow for fine-grained control over specific data types within a ViewModel instance, such as
useViewModelInstanceNumber,useViewModelInstanceString,useViewModelInstanceBoolean, etc.import { useRive, useStateMachineInput } from '@rive-app/react-canvas'; function MyAnimation() { const { rive } = useRive(); const input = useStateMachineInput(rive, 'State Machine 1', 'Input Name'); // Use input to control the animation return null; }Supported React versions
mainRive React is compatible with the following React versions:
^16.8.0through^19.0.0
Use the Rive component to render animations
mainThe
Rivecomponent is the primary way to render Rive animations in a React application. It accepts asrcprop pointing to a Rive asset and allows you to configure artboards, animations, state machines, and layout settings. It also accepts standard HTMLcanvasattributes via spread props.import Rive from '@rive-app/react-canvas'; function MyAnimation() { return ( <Rive src="path/to/your/asset.riv" animations={["idle", "walk"]} stateMachines={["State Machine 1"]} shouldResizeCanvasToContainer /> ); }Use the useViewModel hook
mainThe
useViewModelhook allows you to retrieve a specific ViewModel from a Rive instance.Parameters:
name: (string) The name of the ViewModel to retrieve.useDefault: (boolean) If true, uses the default ViewModel from the Rive instance. Cannot be used ifnameis provided.
Use the useViewModelInstanceProperty hook
mainThe
useViewModelInstancePropertyhook is a base utility for interacting with properties on a RiveViewModelInstance. It manages the lifecycle of property access, keeps React state in sync with property changes, and provides a mechanism for safe property operations (like setting values or triggering events) even during hot-reloading.To use this hook, you must provide a
pathto the property, theviewModelInstance, and anoptionsobject that defines how to interact with that specific property type.import { useViewModelInstanceProperty } from 'rive-react'; // Note: Implementation requires providing specific logic for your property type const { value, setValue, trigger } = useViewModelInstanceProperty( 'myPropertyPath', viewModelInstance, { getProperty: (vm, path) => vm.getProperty(path), getValue: (prop) => prop.value, defaultValue: null, buildPropertyOperations: (safeAccess) => ({ setValue: (newValue) => safeAccess((prop) => prop.value = newValue), trigger: () => safeAccess((prop) => prop.trigger()), }), } );Manage ViewModels with specialized hooks
mainRive React provides a suite of hooks to interact with ViewModels and their specific instance types. This allows you to read or write data to the Rive runtime using strongly typed hooks:
useViewModel: Access the ViewModel.useViewModelInstance: Access a specific ViewModel instance.useGlobalViewModelInstance: Access a global ViewModel instance.
For specific data types, use the corresponding instance hooks:
useViewModelInstanceNumberuseViewModelInstanceStringuseViewModelInstanceBooleanuseViewModelInstanceColoruseViewModelInstanceEnumuseViewModelInstanceTriggeruseViewModelInstanceImageuseViewModelInstanceListuseViewModelInstanceArtboard
Use the useGlobalViewModelInstance hook
mainThe
useGlobalViewModelInstancehook is used to manage a global ViewModel instance.Parameters:
instanceName: (string) Resolve the view model instance with this name.useNew: (boolean) Create a new blank instance of the view model.instance: (ViewModelInstance | null) Register this caller-supplied instance directly.rive: (Rive | null) Registers the instance as the global and schedules a coalesced bind. Required for the hook to register/bind anything.
Use the useViewModelInstance hook
mainThe
useViewModelInstancehook provides access to a specific instance within a ViewModel.Parameters:
name: (string) The name of the instance to retrieve.useDefault: (boolean) Uses the default instance from the ViewModel.useNew: (boolean) Creates a new instance of the ViewModel.rive: (Rive | null) If provided, automatically binds the instance to this Rive instance.