React Wrapper for Vega/Vega-Lite Visualizations
react-vega is a lightweight React component library that wraps vega-embed, enabling the declarative integration of Vega and Vega-Lite visualizations within React applications. Its current stable version is 8.0.0, released in 2024. While specific release cadence isn't explicitly stated, the project maintains an active development presence, aligning with the broader Vega ecosystem's evolution. Key differentiators include providing both a `<VegaEmbed />` component for direct JSX usage and a `useVegaEmbed` hook for more imperative control and access to the Vega View API, facilitating dynamic data updates and event subscriptions without full re-renders. It ships with TypeScript types, offering a robust development experience. Unlike directly using `vega-embed`, `react-vega` handles the React lifecycle integration, ensuring proper mounting, unmounting, and updates of the visualization container.
Common errors
-
Error: "spec.data" is not a function
cause Attempting to update data directly on the `spec` object passed to `VegaEmbed` or `useVegaEmbed` after initial render.fixData in Vega/Vega-Lite is updated via the View API. Use the `useVegaEmbed` hook to get the `view` object and call `view.data('source_name').change(...)`. -
TypeError: Cannot read properties of null (reading 'view')
cause Attempting to access the `view` property from the `useVegaEmbed` result before the embedding process is complete, when `result` is still `null`.fixConditionally render or access `result.view` only when `result` is not `null`. The `useVegaEmbed` hook returns `null` initially and then the `Result` object once `vega-embed` resolves. -
TS2307: Cannot find module 'react-vega' or its corresponding type declarations.
cause The `react-vega` package is not installed, or TypeScript cannot locate its declaration files (e.g., missing `@types/react` or misconfigured `tsconfig.json`).fixEnsure `react-vega`, `vega-embed`, `vega-lite` (and optionally `vega`) are installed via `npm i react-vega vega-embed vega-lite` (and `npm i vega`), and `react` types are present if using TypeScript (`npm i -D @types/react @types/react-dom`). Verify `tsconfig.json` includes `"jsx": "react"` or `"react-jsx"`.
Warnings
- breaking The `data` prop was removed in v8.0.0. Direct data updates via `spec.data` will no longer re-render the view without re-embedding. Use the Vega View API for dynamic data updates.
- breaking The `height` and `width` props were removed in v8.0.0. Changing `spec.width` or `spec.height` will not resize the view without re-embedding. Use the Vega View API for programmatic resizing.
- breaking The `signalListeners` prop was removed in v8.0.0. Subscribing to signal events must now be done via the Vega View API.
- breaking Vega embed `options` are no longer flattened as direct props on the `<VegaEmbed />` component since v8.0.0. They must now be passed as a single object to the `options` prop.
- gotcha Any changes to the `spec` or `options` props of `<VegaEmbed />` (or `useVegaEmbed` params) will cause the entire Vega view to be torn down and re-embedded, leading to a full re-render and loss of interactive state.
Install
-
npm install react-vega -
yarn add react-vega -
pnpm add react-vega
Imports
- VegaEmbed
const VegaEmbed = require('react-vega').VegaEmbed;import { VegaEmbed } from 'react-vega'; - useVegaEmbed
const useVegaEmbed = require('react-vega').useVegaEmbed;import { useVegaEmbed } from 'react-vega'; - VisualizationSpec
import { VisualizationSpec } from 'react-vega';import type { VisualizationSpec } from 'react-vega';
Quickstart
import React from 'react';
import { VegaEmbed } from 'react-vega';
const spec = {
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
description: 'A simple bar chart with embedded data.',
data: { values: [
{a: 'A', b: 28},
{a: 'B', b: 55},
{a: 'C', b: 43},
{a: 'D', b: 91},
{a: 'E', b: 81},
{a: 'F', b: 53},
{a: 'G', b: 19},
{a: 'H', b: 87},
{a: 'I', b: 12}
]},
mark: 'bar',
encoding: {
x: {field: 'a', type: 'nominal', axis: {labelAngle: 0}},
y: {field: 'b', type: 'quantitative'}
}
};
const options = {
actions: false // Disable default actions like 'Export as SVG/PNG'
};
function MyVegaChart() {
return (
<div>
<h1>My Interactive Chart</h1>
<VegaEmbed spec={spec} options={options} />
</div>
);
}
export default MyVegaChart;