# useRupaPlayer

> `const` **useRupaPlayer**: <`S`>(`ref`, `data?`) => [`Player`](/reference/api/rupamotion/player/interfaces/player/)<`S`> | `null`

Mounts the player on the element of `ref` after the render, for markup written in JSX, and destroys it on unmount.
The element and the table are read once, when it mounts: another element needs a new `key`.

## Type Parameters

### S

`S` _extends_ `string` = `string`

## Parameters

### ref

`RefObject`<`Element` | `null`>

A ref to the export's root element (the one with `data-rupa`).

### data?

[`PlayerDataInput`](/reference/api/rupamotion/player/type-aliases/playerdatainput/)

The flow's table (`PlayerData`, e.g. imported from the export's `<base>.player.json`); none to read it
from the page, as `mount` does.

## Returns

[`Player`](/reference/api/rupamotion/player/interfaces/player/)<`S`> | `null`

The `Player`; `null` before it is mounted, on a server, or without an element in `ref` (the development
build warns).

## Example

```tsx
import { useRef } from "react";
import { useRupaPlayer } from "@rupamotion/player/react";
import data from "./card.player.json";

export function Card() {
	const root = useRef<HTMLDivElement>(null);
	const player = useRupaPlayer<"idle" | "open">(root, data);
	return (
		<div ref={root} className="card" data-rupa="">
			<button onClick={() => player?.play("open")}>Open</button>
		</div>
	);
}
```
