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

# View Models & Instances

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>

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

<Steps>
  <Step title="Add a View Model">
    In the Data panel Click the `+` icon and select **View Model**.
  </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>

## 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-transitions-2/RVzTL3yYggO5OStL/images/editor/data-binding/view-models/manage-instances.gif?s=f7e4333d16c7ac92b35e404951faf975" 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-transitions-2/RVzTL3yYggO5OStL/images/editor/data-binding/view-models/open-table-data.png?fit=max&auto=format&n=RVzTL3yYggO5OStL&q=85&s=12e0b54d4ab99abdc7a126e84e47cb94" 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-transitions-2/RVzTL3yYggO5OStL/images/editor/data-binding/view-models/remove-view-model-instance.gif?s=23dfd1e2579d71f588befd693d821a89" 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-transitions-2/RVzTL3yYggO5OStL/images/editor/data-binding/view-models/export-instance.png?fit=max&auto=format&n=RVzTL3yYggO5OStL&q=85&s=9b13a531792ed0b9a0ec94c8dbaaf85e" 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 three common patterns:

| Pattern                                                        |                                                                                                                               |
| :------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------- |
| [Top-Level Instance](#top-level-artboard-view-model-instances) | The data lives on the top-level artboard and its values can be used throughout the Rive file                                  |
| [Nested components](#nested-component-instances)               | The parent view model instance stores a reference to a nested view model instance, which can be applied to a nested component |
| [Stateful nested components](#stateful-components)             | Self-contained, reusable components that store their own unique data                                                          |

### Top-Level Artboard View Model Instances

Attach a View Model Instance directly to the top-level artboard when the data should be available to the entire artboard and its children.

```
GameVM
├─ health
└─ score
└─ avatar
```

<img src="https://mintcdn.com/rive-transitions-2/RVzTL3yYggO5OStL/images/editor/data-binding/view-models/main-vm.png?fit=max&auto=format&n=RVzTL3yYggO5OStL&q=85&s=1adc43f3c8aab245da9b100fdb415b76" alt="Main VM" width="2108" height="582" data-path="images/editor/data-binding/view-models/main-vm.png" />

<UseCase title="Game">
  A game's main view model might contain a score property. This property might be used by various nested components, like the header and within the high scores modal.
</UseCase>

### Nested Component Instances

Components can also have their own 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
```

Each component instance can have its own view model instance attached to it.

<img src="https://mintcdn.com/rive-transitions-2/RVzTL3yYggO5OStL/images/editor/data-binding/view-models/connect-instance-to-nested-component.png?fit=max&auto=format&n=RVzTL3yYggO5OStL&q=85&s=5c8880e7f2b0828dcda7594f4123b9fa" 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>

Because each instance must be created and connected explicitly, this pattern provides strong runtime control at the cost of additional setup.

### 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-transitions-2/RVzTL3yYggO5OStL/images/editor/data-binding/view-models/stateful-components.png?fit=max&auto=format&n=RVzTL3yYggO5OStL&q=85&s=fa7fab0b97dc05ebddbd2c2271d7b6e9" 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).
