# react-vega

> A React wrapper for vega-embed

Latest version **8.0.0** (published 2025-08-27) · 0 weekly downloads

## Install

```sh
npm install react-vega
pnpm add react-vega
yarn add react-vega
bun add react-vega
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 8.0.0 |
| Published | 2025-08-27 |
| First published | 2016-04-15 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 30.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 425 |
| Author | Vega |
| Maintainers | domoritz, kristw, kanitw, kristw-bot |

## Links

- npm: https://www.npmjs.com/package/react-vega
- Repository: https://github.com/vega/react-vega
- Homepage: https://github.com/vega/react-vega#readme
- Issues: https://github.com/vega/react-vega/issues
- npm.io page: https://npm.io/package/react-vega

## Dependencies (1)

- [fast-deep-equal](https://npm.io/package/fast-deep-equal.md) ^3.1.3

## Recent versions

- 8.0.0 (latest) — 2025-08-27
- 7.5.2--canary.497.1504252.0 (canary) — 2022-05-10
- 7.7.1 — 2025-07-19
- 7.7.0 — 2025-07-18
- 7.6.0 — 2022-06-29
- 7.5.2--canary.497.4fdc694.0 — 2022-05-09
- 7.5.2--canary.497.e3127d1.0 — 2022-05-09
- 7.5.1 — 2022-05-06
- 7.5.0 — 2022-04-07
- 7.4.4 — 2021-08-27
- 7.4.3 — 2021-05-01
- 7.4.2 — 2020-11-10
- 7.4.1 — 2020-07-28
- 7.4.0 — 2020-07-20
- 7.3.0 — 2020-03-22
- … 37 more at https://npm.io/package/react-vega/versions

## README

# `react-vega`

A lightweight React wrapper around [`vega-embed`](https://github.com/vega/vega-embed)

## Installation

```bash
npm i react-vega vega-embed vega-lite
```
If you need to use Vega (not Vega-Lite), you will also need to install `vega`.

```bash
npm i vega
```
## Migrating to v8

In v8, the `data` prop was removed. Additionally, you can no longer update data by changing `spec.data` without the view being re-embedded. Instead use the [View API](https://vega.github.io/vega/docs/api/view) to update data. See the [Dynamic data](#1-dynamic-data) recipe for an example.

The `height` and `width` props were removed. Additionally, you can no longer resize the view by changing `spec.width` or `spec.height` without the view being re-embedded. Instead use the [View API](https://vega.github.io/vega/docs/api/view) to resize the view. See the [Programmatically changing width & height](#2-programmatically-changing-width--height) recipe for an example.

The `signalListeners` prop was removed. Instead use the [View API](https://vega.github.io/vega/docs/api/view) to subscribe to signal events. See the [Subscribe to signal events](#3-subscribe-to-signal-events) recipe for an example.

Vega embed options are passed directly via the `options` prop, they are no longer flattened props on the `VegaEmbed` component.

## Quick start

```tsx
import { VegaEmbed } from "react-vega";

function Component() {
  return <VegaEmbed spec={spec} options={options} />;
}
```

or

```tsx
import { useVegaEmbed } from "react-vega";

function Component() {
  const ref = React.useRef<HTMLDivElement>(null);
  const result = useVegaEmbed({ ref, spec, options });
  return <div ref={ref} />;
}
```

## API

### `<VegaEmbed />`

| Prop                  | Type                                   | Default | Notes                                                     |
| --------------------- | -------------------------------------- | ------- | --------------------------------------------------------- |
| `spec` **(required)** | `VisualizationSpec \| string`          | —       | Inline spec or URL. Accepts both Vega & Vega-Lite.        |
| `options`             | [`EmbedOptions`](https://vega.github.io/vega-embed/interfaces/EmbedOptions.html)                         | `{}`    | Passed directly to [`embed()`](https://vega.github.io/vega-embed/functions/default.html).                         |
| `onEmbed`             | `(result: Result) => void`             | —       | Called once [`embed()`](https://vega.github.io/vega-embed/functions/default.html) resolves. See [`Result`](https://vega.github.io/vega-embed/interfaces/Result.html) for details.                            |
| `onError`             | `(err: unknown) => void`               | —       | Called if [`embed()`](https://vega.github.io/vega-embed/functions/default.html) rejects.                          |
| `...divProps`         | `React.HTMLAttributes<HTMLDivElement>` | —       | Forwarded to the `<div>` element used for embedding.                        |
| **ref**               | `React.Ref<HTMLDivElement>`            | —       | Forwarded to the `<div>` element used for embedding. |

### `useVegaEmbed(params)`

```ts
type UseVegaEmbedParams = {
  ref: React.RefObject<HTMLDivElement>;
  spec: VisualizationSpec | string;
  options?: EmbedOptions;
  onEmbed?: (r: Result) => void;
  onError?: (e: unknown) => void;
};

const result: Result | null = useVegaEmbed(params);
```

Returns the current [`Result`](https://vega.github.io/vega-embed/interfaces/Result.html) (or `null` while loading).

## Important
Any changes to `spec` or `options` will cause the view to be torn down and re-embedded. If you need to update the view without re-embedding, use the [View API](https://vega.github.io/vega/docs/api/view). Refer to the [Recipes](#recipes) section for common use cases.

## Recipes

See [storybook](https://vega.github.io/react-vega) for live examples.

### 1. Dynamic data

```tsx
const ref = React.useRef<HTMLDivElement>(null);
const embed = useVegaEmbed({ ref, spec, options: { mode: "vega-lite" } });

useEffect(() => {
  embed?.view.data("values", data).runAsync();
}, [embed, data]);
```

### 2. Programmatically changing width & height

```tsx
const embed = useVegaEmbed({
  ref,
  spec,
});

const changeDimensions = (width: number, height: number) => {
  embed?.view.width(width).height(height).runAsync();
};
```
### 3. Subscribe to signal events

```tsx
const embed = useVegaEmbed({
  ref,
  spec,
});

useEffect(() => {
  const listener = (signal, data) => console.log(signal, data);

  embed?.view.addSignalListener("signal", listener);

  return () => {
    embed?.view.removeSignalListener("signal", listener);
  };
}, [embed]);

```

For more information see the documentation for [vega-embed](https://vega.github.io/vega-embed/) and the [vega View API](https://vega.github.io/vega/docs/api/view).

## Contributing

1. `npm i`
2. `npm run dev` – run the storybook
3. `npm run test` – run the test suite.

---
_Source: https://npm.io/package/react-vega · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
