Parameters - super long title to be truncated
cbHooks
useRive dd
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 theRiveobject at instantiation from the Web runtime.nullandundefinedcan be passed to conditionally display the .riv fileopts- (Optional) See below for a set of options specific torive-react
Parameters
UseRiveParameters
Most of these parameters come from the underlying web runtime configuration items for the Rive object, with the exception of supplying a canvas element. See Rive Parameters for all the parameters you can supply in this object.
If you supply an onLoad callback in the parameters, you may not have access to the rive instance yet. The React runtime uses onLoad internally to setState with the rive instance, and therefore may not be populated by the time it reaches a consumer-supplied callback. We recommend using a useEffect in place of onLoad to reliably use the rive instance if you are looking for a similar method. In a future version of the web runtime, we may supply the rive instance in the parameters of your callback so you can supply an onLoad here.
UseRiveOptions
useDevicePixelRatio- (optional) Iftrue, the hook will scale the resolution of the animation based on the devicePixelRatio. Defaults totrue. NOTE: Requires thesetContainerRefref 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 tofalseuseOffscreenRenderer- (optional) Iftrue, the Rive instance will share (or create if one does not exist) an offscreenWebGLcontext. 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 dedicatedWebGLcontext 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
Return Values
RiveState
canvas- Canvas element the Rive instance is rendered ontocontainer- Container element of the canvas that Rive instance is rendered ontosetCanvasRef- 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.
useStateMachineInput
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
The return value which is the state machine input may not be immediately available due to the need for the rive instance to resolve first. You may want to use a useEffect to watch for when the rive instance and the return value of the useStateMachineInput hook has value
Parameters
rive- The 1st parameter is the Rive object instantiated - this can be retrieved via theuseRivehookstateMachineName?- (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
Return Values
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
See the State Machines page to see more usage of this hook.
useResizeCanvas
The useResizeCanvas hook is an optional utility hook to resize the <canvas> element to its parent container element's size, while also resetting the appropriate surface area size of the canvas as well. This is useful when you don't want to use the useRive hook to render your Rive, and are perhaps using the web JS runtime in your React apps, but still want the ability to scale the <canvas> to its parent appropriately.