Skip to main content
Use the Rive React runtime to render .riv files in React apps. There are three main ways to use Rive in a React app:
  • Hooks - use React hooks to render Rive and control state machines, events, and data binding from React.
  • Rive component - use the default component for simple embeds with limited runtime control.
  • Imperative runtime - use the Web JavaScript runtime directly when you want to create and manage the Rive instance yourself.

Quick Start

1

Install the dependency

npm i --save @rive-app/react-webgl2
This guide uses @rive-app/react-webgl2. Rive also provides other React packages for different renderers. See Choosing a Renderer to choose the package that fits your project.
2

Render the Rive component

Use the returned rive instance with other hooks when you need data binding.
import React, { useEffect } from "react";
import { useRive, useViewModelInstanceNumber } from "@rive-app/react-webgl2";

export default function App() {
  const { rive, RiveComponent } = useRive({
    // Load a local .riv file or use a hosted URL.
    src: "quick_start_health_bar.riv",
    // Be sure to specify the correct state machine (or animation) name
    stateMachines: "State Machine 1",
    // Autoplay the state machine
    autoplay: true,
    // This uses the view model instance defined in Rive
    autoBind: true,
  });

  // Get the bound view model instance
  const vmi = rive?.viewModelInstance;

  // Access the bound numeric `health` field and its setter from the view model instance.
  const { value: health, setValue: setHealth } = useViewModelInstanceNumber(
    "health",
    vmi
  );

  useEffect(() => {
    // Set the health value
    setHealth(10);
  }, [rive, setHealth]);

  return (
    <RiveComponent />
  );
}
The Rive canvas sizes itself based on its container. If nothing appears, make sure the parent element has a defined width and height.