Parameters and Return Values
The
useRive
hook is the recommended way to hook into the Rive runtime for full control, especially when using the Rive State Machine. See below for parameters to pass in and the return values.useRive(riveParams: UseRiveParameters, opts: UseRiveOptions): RiveState
riveParams
- See below for a set of parameters passed to theRive
object at instantiation from the Web runtime.null
andundefined
can be passed to conditionally display the .riv file
UseRiveParameters
src?
- (optional) File path or URL to the .riv file to use. One ofsrc
orbuffer
must be providedbuffer?
- (optional) ArrayBuffer containing the raw bytes from a .riv file. One ofsrc
orbuffer
must be providedartboard?
- (optional) Name of the artboard to useanimations?
- (optional) Name or list of names of animations to playstateMachines?
- (optional) Name or list of names of state machines to loadlayout?
- (optional) Layout object to define how animations are displayed on the canvas. See our web runtime docs for more detailsautoplay?
- (optional) If true, the animation will automatically start playing when loaded. Defaults to falseonLoad?
- (optional) Callback that gets fired when the .rive file loadsonLoadError?
- (optional) Callback that gets fired when an error occurs loading the .riv fileonPlay?
- (optional) Callback that gets fired when the animation starts playingonPause?
- (optional) Callback that gets fired when the animation pausesonStop?
- (optional) Callback that gets fired when the animation stops playingonLoop?
- (optional) Callback that gets fired when the animation completes a looponStateChange?
- (optional) Callback that gets fired when a state change occurs
Most of these parameters come from the underlying web runtime configuration items for the Rive object. See the types here.
UseRiveOptions
useDevicePixelRatio
- (optional) Iftrue
, the hook will scale the resolution of the animation based on the devicePixelRatio. Defaults totrue
. NOTE: Requires thesetContainerRef
ref callback to be passed to an element wrapping a canvas element. If you use theRiveComponent
, then this will happen automaticallyfitCanvasToArtboardHeight
- (optional) Iftrue
, then the canvas will resize based on the height of the artboard. Defaults tofalse
useOffscreenRenderer
- (optional) Iftrue
, the Rive instance will share (or create if one does not exist) an offscreenWebGL
context. This allows you to display multiple Rive animations on one screen to work around some browser limitations regarding multiple concurrent WebGL contexts. Iffalse
, each Rive instance will have its own dedicatedWebGL
context and you may need to be cautious of the browser limitations just mentioned. We recommend not changing this default prop, so you don't have to manage WebGL contexts. Destroying a React component does not guarantee the browser cleans up the WebGL context that was created when the canvas was mounted. Only relevant when using@rive-app/react-webgl
. Defaults totrue
RiveState
canvas
- Canvas element the Rive instance is attached tosetCanvasRef
- Ref callback to be passed to the canvas elementsetContainerRef
- Ref callback to be passed to the container element of the canvas. This is optional, however, if not used then the hook will not take care of automatically resizing the canvas to its outer container if the window resizesrive
- Newly created Rive instance from the Web runtimeRiveComponent
- JSX element to render the Rive instance in the DOM
In most cases, you will just need to grab the
RiveComponent
and rive
return values from the useRive
hook. Setting the canvas ref and container ref is only needed if you need to control the canvas/containing element yourself.The
useStateMachineInput
hook is the recommended way to grab references to Rive State Machine inputs, both for reading input values, and setting (or triggering) them. See below for parameters to pass in and the return value.useStateMachineInput(rive: Rive | null, stateMachineName?: string, inputName?: string, initialValue?: number | boolean): StateMachineInput | null
rive
- The 1st parameter is the Rive object instantiated - this can be retrieved via theuseRive
hookstateMachineName?
- (optional) Name of the state machine to grab the input frominputName?
- (optional) Name of a single state machine input to grab a reference toinitialValue?
- (optional) Initial value to set on the input
This hook returns a default instance of a
StateMachineInput
.StateMachineInput
name
(get) - Access the name of the inputvalue
(get and set) - Access the value of the input, and set the value of the input via this propertyfire()
- Fires off a trigger input
The
RiveComponent
default export and the RiveComponent
returned from the useRive
hook are both to be rendered in the JSX of a component. As noted previously, all attributes and event handlers that can be passed to a canvas
element can also be passed to the Rive
component and used in the same manner.One thing to note is that
style
/className
props set on the component will be passed onto the containing <div>
element, rather than the underlying <canvas>
itself. The reason for this is that the containing <div>
element handles resizing and layout for you, and thus, all styles should be passed onto this element.
The <canvas>
element will still receive any other props passed into the component, such as aria-*
attributes, role
's, etc.
Last modified 8mo ago