Skip to content
Limited timeStarter is now $9 a month instead of $20.See plans
charmotion

How to Use Rive Data Binding & ViewModels in React and Next.js

By the Charmotion team · Updated · How we write guides

Cover illustration for the guide: How to Use Rive Data Binding & ViewModels in React and Next.js, with Charmotion demo characters

Use Rive data binding by loading the .riv file with the React runtime, reading the ViewModel instance once the Rive object is ready, and setting its typed properties from component state, such as a number named mood. In Next.js, put that component in a Client Component so it runs in the browser.

Key takeaways

  • Data binding exposes typed ViewModel properties that React code can read and write while the animation runs.
  • Install @rive-app/react-canvas, load the file with useRive, and wait for the Rive object before reading values.
  • Map app state to numbers such as mood, and write them in a useEffect that depends on that state.
  • In Next.js, keep the component in a Client Component, and use next/dynamic with ssr set to false only from a Client Component.
  • Check artboard and property names against the editor, because the runtime looks them up by exact name.

What is Rive data binding and what is a ViewModel?

Data binding is how a Rive file exposes values to the app that runs it. A designer defines a ViewModel in the Rive editor: a named set of typed properties. The app then creates a ViewModel instance, which holds the live values, and reads or writes them while the animation plays.

Rive's property types include Number, String, Boolean, Color, Enum, Trigger, Image, nested View Model, and List (see Rive's property types page). For a mascot, a number that selects the mood, a string for a speech bubble, and a boolean that hides decorative accents cover most needs.

Data binding is different from a state machine input. A state machine input is a single value the state machine reads, such as a number that drives transitions. A ViewModel property is a named value on an instance that your code sets from data it already has, such as a user's status, a loading flag or a balance. For the broader state machine model, see Lottie vs Rive state machines and data binding.

How to use Rive Data Binding ViewModel in React and Next.js

The React path has four parts: a .riv file with a ViewModel, the React runtime, a hook that loads the file, and an effect that writes values. For most React apps the runtime is @rive-app/react-canvas, which Rive recommends unless you specifically need a WebGL renderer. Install it first:

npm install @rive-app/react-canvas

The useRive hook takes the file source, the artboard name and the state machine name, and returns a Rive object plus a component that draws it. Replace the names in the example with the ones in your file.

'use client';
import { useEffect } from 'react';
import { useRive } from '@rive-app/react-canvas';

export default function Mascot({ mood }: { mood: number }) {
  const { rive, RiveComponent } = useRive({
    src: '/mascot.riv',
    artboard: 'Mascot',
    stateMachines: 'CharacterStateMachine',
    autoplay: true,
  });

  useEffect(() => {
    if (!rive) return;
    const vmi = rive.viewModelInstance;
    if (!vmi) return;
    const moodProp = vmi.number('mood');
    if (moodProp) moodProp.value = mood;
  }, [rive, mood]);

  return <RiveComponent />;
}

Three details matter. The rive object is null until the file loads, so the effect returns early. The effect reruns when mood changes, so each new value is written once. And number('mood') looks up the property by name, so the name must match the one in the editor exactly.

How do you set ViewModel values from React state?

Keep the React side simple: store a readable state name, convert it to the number the file expects, and pass the number down. A single lookup object keeps the mapping in one place.

const MOOD = { idle: 0, happy: 1, wave: 2, loading: 5, sad: 4 };

If your file came from Charmotion, the CharacterViewModelMachine has a number property named mood, and the numbers are fixed:

MoodNumber
idle0
happy1
wave2
thinking3
sad4
loading5
listening6
talking7
sleeping8
attention9

Moods blend into each other when the number changes, so a jump from 0 to 5 shows a transition rather than a cut. Characters have no one-shot triggers, so show a success with happy (1) and an error with sad (4), and drive loading with 5 while a request runs.

The Charmotion file also has a CharacterStateMachine with a number input named mood. Either route works, but choose one per value so the code stays easy to follow. Artboard and machine names come from your export, so copy them from the file rather than from this guide.

When do you need setViewModelInstance and bind()?

The default path reads the instance that is already bound to the artboard. Sometimes a file holds several instances, for example one mascot setup for free accounts and another for paid accounts, and your code must choose which one drives the animation. The Web runtime covers that case.

The Web runtime exposes a viewModelInstance getter for the currently bound instance and a viewModel(path) method that returns a ViewModelInstance. setViewModelInstance assigns an instance to the state machine without binding it, and bind() applies the instances that were set. getBindableArtboard(name) returns a BindableArtboard, and setting its viewModel property binds an instance to it.

If the getter returns null after the file loads, the file has no default instance bound, and this manual path is the fix. Check the Web runtime parameters reference for exact signatures before you copy them, because method names differ between runtime versions.

Where should the Rive component run in Next.js?

The component calls useRive, which draws to a canvas and uses browser APIs. In the App Router, that makes it a Client Component, so the file starts with 'use client' as in the example above. A Server Component can import that file and pass plain props such as mood, which is the usual pattern when the value comes from the server.

If you want to keep the mascot out of the server-rendered HTML, import it with next/dynamic and ssr: false from inside a Client Component. Next.js does not allow that option in Server Components, so the wrapper has to be a Client Component.

'use client';
import dynamic from 'next/dynamic';

const Mascot = dynamic(() => import('./Mascot'), { ssr: false });

export default function HeroMascot({ mood }: { mood: number }) {
  return <Mascot mood={mood} />;
}

Values that change in the browser, such as a button click or a streamed chat status, belong in client state. Pass them down to the Client Component as props from there. For the App Router details, see Rive in the Next.js App Router.

Which React hook should you use, and which version?

The React package gives you two hooks that matter here. useRive loads the file and returns the Rive object and the component. useStateMachineInput takes the Rive object, a state machine name and an input name, and returns the input once the Rive object is ready and the input exists. That suits a single transition value. For ViewModel properties, read them from rive.viewModelInstance, as shown earlier.

Rive's React docs still describe useStateMachineInput for state machine inputs. Before you choose a hook for new code, check the package's release notes for its current status, and keep @rive-app/react-canvas on a recent release, since the runtime is updated regularly.

From version 2.x onward, the React runtime wraps either the WebGL or the Canvas dependency through two packages, and @rive-app/react-canvas is the recommended one (see the React migration guides). If you are moving an older project, check which package it imports before you copy any hook call from an older tutorial. For a comparison of the two binding styles, see Rive data binding vs state machine inputs.

How does data binding differ across other Rive runtimes?

The same ViewModel can be read in other runtimes, but the calls differ, and each runtime has its own version rules.

  • React Native: the useViewModelInstance hook creates an instance from a RiveFile, a ViewModel or a RiveViewRef. Its async option requires @rive-app/react-native 0.4.18 or later.
  • Flutter: a ViewModel can create instances as blank, default, by index or by name. From Flutter 0.15.0, binding happens when a RiveWidgetController is created, and the separate dataBind call was removed. That release needs Flutter 3.32.0 or later.
  • Apple: from v6.28.0, authored default ViewModel instances bind automatically. DataBind is deprecated, and Rive.viewModelInstance returns nil with the new initializers.
  • Android and Unity: Android v11.13.0 added font data binding, and Unity v0.4.5 added Fonts and Global View Models, according to Rive's runtime update announcements.

If your team also ships a Vue or SwiftUI version, the setup for each is in Rive in Vue 3 and Nuxt and How to Use Rive in SwiftUI. Keep property names identical across platforms, so one editor file serves every client.

How to use Rive Data Binding ViewModel in React and Next.js

  1. Add a ViewModel in the Rive editor. Create a ViewModel with a number property named mood, and bind it to the artboard you want to control, then export the .riv file.
  2. Install the React runtime. Run npm install @rive-app/react-canvas in your project.
  3. Load the file with useRive. Call useRive with the file source, the artboard name and the state machine name, and render the RiveComponent it returns.
  4. Read the ViewModel instance. When the rive object is ready, read rive.viewModelInstance and get the number property with number('mood'). If the instance is null, bind one using the Web runtime methods.
  5. Write values from state. Store the mood in React state, then set moodProp.value inside a useEffect that depends on rive and mood.
  6. Use a Client Component in Next.js. Start the file with 'use client', and load it from a Server Component only through props, or with next/dynamic and ssr set to false from a Client Component.

Frequently asked questions

Does useStateMachineInput still work next to ViewModel properties?

Yes, they are separate. useStateMachineInput reads and sets named state machine inputs, while ViewModel properties are read through the instance. A Charmotion file includes both a CharacterStateMachine number input and a CharacterViewModelMachine mood property, so you can drive mood either way. Pick one route per value to keep the code easy to follow.

Why is rive.viewModelInstance null?

Check three things. First, the file must load: rive is null until the Rive object is ready, so the effect returns early. Second, the instance must be bound. If the file has no default instance, use setViewModelInstance and bind() as described in the Web runtime reference. Third, the names must match. viewModelInstance and number('mood') look up exact names from the editor, so a rename there breaks the lookup.

Do I need a paid Rive plan to use data binding in my app?

This guide does not cover Rive plan limits, so check the current terms at /guides/is-rive-free-pricing. The runtimes are open source and play any .riv file. Exports from Rive's Free plan play a Rive splash screen, which is the main difference you will notice when shipping.

Can I use the same ViewModel in React Native?

Yes. The React Native runtime has a useViewModelInstance hook that creates an instance from a RiveFile, a ViewModel or a RiveViewRef. Its async option requires @rive-app/react-native 0.4.18 or later. The property names you set in the editor are the same ones your React Native code reads.

Sources

Related guides