View Models
View models describe a set of properties, but cannot themselves be used to get or set values - that is the role of view model instances. To begin, we need to get a reference to a particular view model. This can be done either by index, by name, or the default for a given artboard, and is done from the Rive file. The default option refers to the view model assigned to an artboard by the dropdown in the editor.- New Runtime (Recommended)
- Legacy Runtime
View Model Instances
Once we have a reference to a view model, it can be used to create an instance. When creating an instance, you have four options:-
Create a blank instance - Fill the properties of the created instance with default values as follows:
- Create the default instance - Use the instance labelled “Default” in the editor. Usually this is the one a designer intends as the primary one to be used at runtime.
- Create by index - Using the order returned when iterating over all available instances. Useful when creating multiple instances by iteration.
- Create by name - Use the editor’s instance name. Useful when creating a specific instance.
In some samples, due to the wordiness of “view model instance”, we use the abbreviation “VMI”, as well as “VM” for “view model”.
- New Runtime (Recommended)
- Legacy Runtime
Use the All lookup options combine with You can also get the auto-bound instance from a
useViewModelInstance hook to create a view model instance. You can pass a RiveFile, ViewModel, or RiveViewRef as the source.Pass async: true to create the instance asynchronously. The hook returns { instance, isLoading, error } and the instance is not available on the first render, so gate rendering on it. Without async: true, the instance is created through deprecated synchronous APIs that block the JS thread.async: true:async: true is available in @rive-app/react-native@0.4.18 and later. The synchronous creation path is deprecated and will be removed in a future release.RiveViewRef. The hook waits up to five seconds for the view’s auto-bound instance to become available:Binding
The created instance can then be assigned to a state machine or artboard. This establishes the bindings set up at edit time. It is preferred to assign to a state machine, as this will automatically apply the instance to the artboard as well. Only assign to an artboard if you are not using a state machine, i.e. your file is static or uses linear animations.The initial values of the instance are not applied to their bound elements until the state machine or artboard advances.
- New Runtime (Recommended)
- Legacy Runtime
For React Native, no additional steps are needed to bind the view model instance to the Rive component. Pass the instance to the
dataBind prop on RiveView:Auto-Binding
Alternatively, you may prefer to use auto-binding. This will automatically bind the default view model of the artboard using the default instance to both the state machine and the artboard. The default view model is the one selected on the artboard in the editor dropdown. The default instance is the one marked “Default” in the editor.- New Runtime (Recommended)
- Legacy Runtime
Auto-binding is available through the You can also bind by name:Or bind a specific instance:
DataBindMode enum. You can pass DataBindMode.Auto to the dataBind prop:Properties
A property is a value that can be read, set, or observed on a view model instance. Properties can be of the following types:
For more information on version compatibility, see the Feature Support page.
Listing properties
Property descriptors can be inspected on a view model to discover at runtime which are available. These are not the mutable properties themselves though - once again those are on instances. These descriptors have a type and name.- New Runtime (Recommended)
- Legacy Runtime
Use
getPropertiesAsync on a ViewModel to list its properties. Each entry has a name and a type, such as 'number', 'string', 'boolean', 'color', 'enum', 'trigger', 'list', 'viewModel', 'assetImage', or 'artboard'.ViewModelInstance also has getPropertiesAsync, but only for instances created from a ViewModel or with useViewModelInstance. For nested instances, list items, and instances obtained from a view, it rejects; query the ViewModel instead.getPropertiesAsync requires 0.5.0 or later.Reading and writing properties
References to these properties can be retrieved by name or path. Some properties are mutable and have getters, setters, and observer operations for their values. Getting or observing the value will retrieve the latest value set on that property’s binding, as of the last state machine or artboard advance. Setting the value will update the value and all of its bound elements.After setting a property’s value, the changes will not apply to their bound elements until the state machine or artboard advances.
- New Runtime (Recommended)
- Legacy Runtime
Use the specific hooks for each property type to get and set property values:The Outside of hooks, use the property accessors on a
useRiveBoolean: Read/write boolean propertiesuseRiveString: Read/write string propertiesuseRiveNumber: Read/write number propertiesuseRiveColor: Read/write color properties with hex string or RGBA supportuseRiveEnum: Read/write enum propertiesuseRiveTrigger: Fire trigger events with optional callbacks
value, a setter function (setValue or trigger), and an error if the property can’t be found or read. The value is undefined until the first value arrives from the runtime.The
setValue function allows you to pass a function that receives the previous value, similar to React’s setState pattern. This is useful when you need to update a value based on its current state:value returned by each hook will update automatically when the property changes in the Rive graphic.In
0.5.0 and later, a misspelled property path is not detected when you look the property up. It surfaces as the hook’s error, or as a rejected getValueAsync() promise.ViewModelInstance. Read values with getValueAsync() and write them with set(), or with setValueAsync() if you need to wait for the write to complete:Nested property paths
View models can have properties of type view model, allowing for arbitrary nesting. You can chain property calls on each instance starting from the root until you get to the property of interest. Alternatively, you can do this through a path parameter, which is similar to a URI in that it is a forward slash delimited list of property names ending in the name of the property of interest.- New Runtime (Recommended)
- Legacy Runtime
Access nested properties by providing the full path (separated by To work with a nested view model instance directly, use To swap a nested view model for another instance of the same view model, use
/) as the first argument to the property hooks.viewModelAsync with the property name or path:replaceViewModel:In
0.5.0 and later, replaceViewModel is supported on iOS only. On Android it logs a warning and does nothing.Observability
You can observe changes over time to property values, either by using listeners or a platform equivalent method. Once observed, you will be notified when the property changes are applied by a state machine advance, whether that is a new value that has been explicitly set or if the value was updated as a result of a binding.- New Runtime (Recommended)
- Legacy Runtime
Values are observed automatically through hooks. When a property’s value changes within the Rive instance (either because you set it via a hook or due to an internal binding), the
value returned by the corresponding hook updates. This state change triggers a re-render of your React component, allowing you to react to the new value.For Triggers, you can provide an onTrigger callback directly to the useRiveTrigger hook, which fires when the trigger is activated in the Rive instance.The
useRiveTrigger hook returns a trigger function that can be called to fire the trigger. This hook accepts an optional onTrigger callback in its third parameter that will be executed when the trigger is fired.Images
Image properties let you set and replace raster images at runtime, with each instance of the image managed independently. For example, you could build an avatar creator and dynamically update features — like swapping out a hat — by setting a view model’s image property.- New Runtime (Recommended)
- Legacy Runtime
Image properties can be set using the Other image loading options on
imageProperty method on a ViewModelInstance and the RiveImages utility for loading images.In
0.5.0 and later, addListener on image properties is not supported: it returns an unsubscribe function but the listener is never called.RiveImages:Lists
List properties let you manage a dynamic set of view model instances at runtime. For example, you can build a to-do app where users can add and remove tasks in a scrollable Layout. See the Editor section on creating data bound lists. A single list property can include different view model types, with each view model tied to its own Component, making it easy to populate a list with a variety of Component instances. With list properties, you can:- Add a new view model instance (optionally at an index)
- Remove an existing view model instance (optionally by index)
- Swap two view model instances by index
- Get the size of a list
- New Runtime (Recommended)
- Legacy Runtime
Use the All
useRiveList hook to manage list properties on view model instances.useRiveList functions except length return promises. length starts at 0 and updates after each mutation made through the hook.In
0.5.0 and later, list properties don’t report changes made outside of useRiveList, such as by the state machine. To read the current state, call getLengthAsync() and getInstanceAtAsync(index) on the list property from instance.listProperty('todos').Artboards
Artboard properties allows you to swap out entire components at runtime. This is useful for creating modular components that can be reused across different designs or applications, for example:- Creating a skinning system that supports a large number of variations, such as a character creator where you can swap out different body parts, clothing, and accessories.
- Creating a complex scene that is a composition of various artboards loaded from various different Rive files (drawn to a single canvas/texture/widget).
- Reducing the size (complexity) of a single Rive file by breaking it up into smaller components that can be loaded on demand and swapped in and out as needed.
- New Runtime (Recommended)
- Legacy Runtime
Artboard properties work with the The artboard can come from a different
BindableArtboard class. Use getBindableArtboard on a RiveFile to create a bindable reference, then set it on the artboard property.RiveFile than the one bound to the view, which lets you keep swappable content in a separate file. getBindableArtboard throws if the file has no artboard with that name. Use getArtboardNamesAsync() on the RiveFile to list the available names.Enums
Enums properties come in two flavors: system and user-defined. In practice, you will not need to worry about the distinction, but just be aware that system enums are available in any Rive file that binds to an editor-defined enum set, representing options from the editor’s dropdowns, where user-defined enums are those defined by a designer in the editor. Enums are string typed. The Rive file contains a list of enums. Each enum in turn has a name and a list of strings.- New Runtime (Recommended)
- Legacy Runtime
You can access enum properties using the To list the enums defined in a file and their possible values, use
useRiveEnum hook. The hook returns the current value and a setter function.getEnums:getEnums requires 0.5.0 or later.Examples
- New Runtime (Recommended)
- Legacy Runtime
See the example app for data binding demos with the new runtime.