> ## Documentation Index
> Fetch the complete documentation index at: https://rive-advanced-topics.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# View Models & Instances

export const YouTube = ({id, timestamp}) => {
  const videoSrc = timestamp ? `https://www.youtube.com/embed/${id}?start=${timestamp}` : `https://www.youtube.com/embed/${id}`;
  return <iframe width="100%" height="400" src={videoSrc} title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerPolicy="strict-origin-when-cross-origin" allowFullScreen />;
};

export const UseCase = ({title, children}) => {
  return <aside class="callout my-4 px-5 py-4 overflow-hidden rounded-2xl flex gap-3 border border-neutral-200 bg-neutral-50 dark:border-neutral-700 dark:bg-white/10" data-callout-type="info">
      <div class="mt-0.5 w-4" data-component-part="callout-icon">
        <svg width="11" height="14" viewBox="0 0 11 14" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="text-neutral-800 dark:text-neutral-300 w-3.5 h-auto" aria-label="Tip"><path d="M3.12794 12.4232C3.12794 12.5954 3.1776 12.7634 3.27244 12.907L3.74114 13.6095C3.88471 13.8248 4.21067 14 4.46964 14H6.15606C6.41415 14 6.74017 13.825 6.88373 13.6095L7.3508 12.9073C7.43114 12.7859 7.49705 12.569 7.49705 12.4232L7.50055 11.3513H3.12521L3.12794 12.4232ZM5.31288 0C2.52414 0.00875889 0.5 2.26889 0.5 4.78826C0.5 6.00188 0.949566 7.10829 1.69119 7.95492C2.14321 8.47011 2.84901 9.54727 3.11919 10.4557C3.12005 10.4625 3.12175 10.4698 3.12261 10.4771H7.50342C7.50427 10.4698 7.50598 10.463 7.50684 10.4557C7.77688 9.54727 8.48281 8.47011 8.93484 7.95492C9.67728 7.13181 10.1258 6.02703 10.1258 4.78826C10.1258 2.15486 7.9709 0.000106649 5.31288 0ZM7.94902 7.11267C7.52078 7.60079 6.99082 8.37878 6.6077 9.18794H4.02051C3.63739 8.37878 3.10743 7.60079 2.67947 7.11294C2.11997 6.47551 1.8126 5.63599 1.8126 4.78826C1.8126 3.09829 3.12794 1.31944 5.28827 1.3126C7.2435 1.3126 8.81315 2.88226 8.81315 4.78826C8.81315 5.63599 8.50688 6.47551 7.94902 7.11267ZM4.87534 2.18767C3.66939 2.18767 2.68767 3.16939 2.68767 4.37534C2.68767 4.61719 2.88336 4.81288 3.12521 4.81288C3.36705 4.81288 3.56274 4.61599 3.56274 4.37534C3.56274 3.6515 4.1515 3.06274 4.87534 3.06274C5.11719 3.06274 5.31288 2.86727 5.31288 2.62548C5.31288 2.38369 5.11599 2.18767 4.87534 2.18767Z"></path></svg>
      </div>
      <div class="text-sm prose dark:prose-invert min-w-0 w-full [&_kbd]:bg-background-light dark:[&_kbd]:bg-background-dark [&_code]:!text-current [&_kbd]:!text-current [&_a]:!text-current [&_a]:border-current [&_strong]:!text-current text-neutral-800 dark:text-neutral-300" data-component-part="callout-content">
        <strong>Example{title && ` - ${title}`}</strong>

        {children}
      </div>
    </aside>;
};

A View Model defines the structure of your data. It acts as a reusable blueprint that contains a collection of properties, such as a car's color, speed, and damage.

Each property has a type, such as Number, String, Boolean, Color, Enum, or List (For more information, see [View Model Properties](/editor/data-binding/view-model-properties)).

View Models do not store values themselves. Instead, values are stored in one or more View Model Instances that are created from the View Model.

For example, if you were building a racing game, you might create a Car View Model with the following [properties](/editor/data-binding/view-model-properties):

```jsx theme={null}
Car (View Model)
  color: Color
  damage: number
  make: string
  speed: number
```

A single View Model can be used to create many View Model Instances. Each instance shares the same structure but stores its own values.

```jsx theme={null}
Car1 (View Model Instance)
  color: 0xFFFF0000
  damage: 0
  make: "Subaru"
  speed: 0

Car2 (View Model Instance)
  color: 0xFFFF00FF
  damage: .9
  make: "Honda"
  speed: 0
```

The properties in your instances can then be bound to elements in your designs, allowing text, images, colors, animations, state machines, and other properties to react to data changes automatically.

### Creating a View Model

A View Model defines the structure of your data. Before creating View Model Instances or setting up data bindings, you'll first need to create a View Model and add properties to it.

<Note>
  New Rive files come with a default view model called "ViewModel1" that is already attached to the main artboard.
</Note>

<Steps>
  <Step title="Add a View Model">
    In the Data panel, click the `+` icon and select **View Model** or [**Global View Model**](#global-view-model-instances).
  </Step>

  <Step title="Add Properties">
    In the Data panel, click the **Add View Model Property** button next to the view model name and select your [property type](/editor/data-binding/property-types).
  </Step>
</Steps>

<img src="https://mintcdn.com/rive-advanced-topics/eJvnNDd5OACRAz20/images/editor/data-binding/view-models/create-view-model.gif?s=bb0daacf63c39f42afa06705751b5900" alt="Create a View Model" width="640" height="335" data-path="images/editor/data-binding/view-models/create-view-model.gif" />

## Creating and Editing Instances

Once your View Model has been created, you can create one or more View Model Instances that store actual values for those properties.

### Creating a View Model Instance

A View Model can have multiple instances. Each instance contains its own set of property values while sharing the same structure defined by the View Model.

<Note>
  When creating a new View Model, Rive automatically creates a View Model Instance named "Instance" so you can immediately begin testing bindings and previewing data.
</Note>

<img src="https://mintcdn.com/rive-advanced-topics/eJvnNDd5OACRAz20/images/editor/data-binding/view-models/manage-instances.gif?s=73d7d2c37727b3e1e3b2e6bd91c874c5" alt="Create a View Model Instance" width="640" height="225" data-path="images/editor/data-binding/view-models/manage-instances.gif" />

<Steps>
  <Step title="View existing instances">
    Select a View Model in the Data panel, then click the Controls icon next to the instance name in the Inspector.

    <Tip>
      To see all of your view model instances and values at once, click the Open Table Data button.

      <img src="https://mintcdn.com/rive-advanced-topics/eJvnNDd5OACRAz20/images/editor/data-binding/view-models/open-table-data.png?fit=max&auto=format&n=eJvnNDd5OACRAz20&q=85&s=01f367fd2d0f4763ab6c7ba07e776c7f" alt="Open table data" width="2390" height="492" data-path="images/editor/data-binding/view-models/open-table-data.png" />
    </Tip>
  </Step>

  <Step title="Add an instance">
    Click the `+` icon to add a new instance.
  </Step>

  <Step title="Name the instance">
    Double click the instance's name to rename it.
  </Step>

  <Step title="Update the instance data">
    With the instance selected, change any of its properties.

    Note that this changes **only** the values on the selected view model instance.
  </Step>
</Steps>

### Removing Instances

Click the `-` icon next to the instance name to delete it.

<img src="https://mintcdn.com/rive-advanced-topics/eJvnNDd5OACRAz20/images/editor/data-binding/view-models/remove-view-model-instance.gif?s=133ec47920cfb05ee28bfacbf9ce07ac" alt="Remove a View Model Instance" width="640" height="233" data-path="images/editor/data-binding/view-models/remove-view-model-instance.gif" />

<Note>
  Deleting a View Model Instance does not remove the View Model or its properties. It only removes that instance and its stored values.
</Note>

### Exporting Instances

By default, exported View Model Instances are included in the .riv file. Disable Export Instance when the data is only needed during development, not in your application at runtime.

<img src="https://mintcdn.com/rive-advanced-topics/eJvnNDd5OACRAz20/images/editor/data-binding/view-models/export-instance.png?fit=max&auto=format&n=eJvnNDd5OACRAz20&q=85&s=1dff8095ca8d45ad44bffdc048d32619" alt="Export an Instance" width="1948" height="468" data-path="images/editor/data-binding/view-models/export-instance.png" />

<UseCase title="High Score">
  Let's say you're building a high score screen in Rive. While designing, you'll use fake score data, but when your game is live, the high scores will be populated using real data from a database or another source.

  In this case embedding the example data just increases your .riv file size.
</UseCase>

## Connecting View Model Instances to Artboards

Before binding data, you'll need to decide where the data should live.

Rive supports a few common patterns:

| Pattern                                                             |                                                                                                                  |
| :------------------------------------------------------------------ | :--------------------------------------------------------------------------------------------------------------- |
| [Global Instances](#global-view-model-instances)                    | Global view models and their properties are directly accessible to all artboards                                 |
| [Instances attached to artboards](#instances-attached-to-artboards) | The view model instance is attached to the artboard and its values can be used by that artboard and its children |
| [Instances as nested properties](#instances-as-nested-properties)   | The parent view model instance stores a reference to a nested view model instance                                |
| [Stateful nested components](#stateful-components)                  | Self-contained, reusable components that store their own unique data                                             |

### Global View Model Instances

Global view models are not connected to a specific artboard. Instead, the view model instance and its properties are directly accessible to all artboards.

<YouTube id="SPrxwHfPyP4" />

<UseCase title="Game">
  A game's main view model might contain a `brandColor` or `score` property. This property might be used by various nested components, like the header, on buttons, or on a high scores screen.
</UseCase>

### Instances Attached to Artboards

View Model instances can be attached to a specific artboard or nested component. This is useful when only that artboard and its children need access to that data.

```
EnemyVM
├─ health
```

<UseCase title="Game">
  An Enemy view model might contain a `health` property. This property might be used by the enemy itself, but not by its parent.
</UseCase>

### Instances as Nested Properties

View Models can have properties that are View Model instances.

In this pattern, the parent View Model stores references to child instances, either directly or through a [list](/editor/data-binding/lists).

```
GameVM
├─ health
└─ score
└─ avatar
└─ inventory (stores an instance of InventoryVM)

InventoryVM
├─ Sword
├─ Shield
└─ Potion
```

<img src="https://mintcdn.com/rive-advanced-topics/eJvnNDd5OACRAz20/images/editor/data-binding/view-models/connect-instance-to-nested-component.png?fit=max&auto=format&n=eJvnNDd5OACRAz20&q=85&s=d23c3887f5b089cbb7924ce1683207a7" alt="Attach instance to component" width="2110" height="682" data-path="images/editor/data-binding/view-models/connect-instance-to-nested-component.png" />

This approach is useful when a parent artboard needs to manage a collection of items while still allowing each component to maintain its own data.

<UseCase title="Inventory Grid">
  An inventory screen might contain a list of Item View Model Instances, with each inventory slot component bound to a different item.
</UseCase>

### Stateful Components

A stateful component is an instance of a component that maintains its own data.

Unlike traditional component View Model Instances, these instances do not need to be created or referenced by the parent View Model. Instead their properties are exposed in the sidebar.

<img src="https://mintcdn.com/rive-advanced-topics/eJvnNDd5OACRAz20/images/editor/data-binding/view-models/stateful-components.png?fit=max&auto=format&n=eJvnNDd5OACRAz20&q=85&s=80e07c7c246d3ace7605ea2db40403da" alt="Stateful components" width="2112" height="720" data-path="images/editor/data-binding/view-models/stateful-components.png" />

For more information, see [Stateful Components](/editor/data-binding/stateful-components).
