Registry / web-framework / react-vega

react-vega

JSON →
library8.0.0jsnpmunverified

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.

npm install react-vega
INSTALL
IMPORT
SIG · REACT-VEGA
R
react-vega
web-frameworkjavascriptv8.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

VegaEmbed
import { VegaEmbed } from 'react-vega';
const VegaEmbed = require('react-vega').VegaEmbed;
Primary component for embedding. CommonJS `require` is not officially supported and may lead to issues with modern build setups. The library primarily targets ESM.
useVegaEmbed
import { useVegaEmbed } from 'react-vega';
const useVegaEmbed = require('react-vega').useVegaEmbed;
React hook for more granular control over the Vega view, especially for dynamic updates. Like `VegaEmbed`, it's designed for ESM.
VisualizationSpec
import type { VisualizationSpec } from 'react-vega';
import { VisualizationSpec } from 'react-vega';
This is a TypeScript type import. Attempting to import it as a value will result in a runtime error or build failure in strict environments. It is re-exported from `vega-embed`.

This example demonstrates embedding a static Vega-Lite bar chart using the `VegaEmbed` component.

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;
Debug
Known issues
breakingThe `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.
fix
Migrate to using the `useVegaEmbed` hook to access the Vega View API and update data using `view.data('source_name').insert/remove/change()`. Refer to the 'Dynamic data' recipe in the documentation.
affects: >=8.0.0
breakingThe `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.
fix
Utilize the `useVegaEmbed` hook to obtain the Vega View API and call `view.resize()` or `view.width()/height()` directly. See the 'Programmatically changing width & height' recipe.
affects: >=8.0.0
breakingThe `signalListeners` prop was removed in v8.0.0. Subscribing to signal events must now be done via the Vega View API.
fix
With the `useVegaEmbed` hook, get the `view` object from the result and use `view.addSignalListener('signalName', handlerFunction)` to subscribe to signals. Check the 'Subscribe to signal events' recipe.
affects: >=8.0.0
breakingVega 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.
fix
Consolidate all `vega-embed` specific options into a single object and pass it to the `options` prop: `<VegaEmbed spec={mySpec} options={{ actions: false, renderer: 'svg' }} />`.
affects: >=8.0.0
gotchaAny 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.
fix
For dynamic updates to data, dimensions, or signal listeners that should preserve interaction state, use the Vega View API obtained via the `useVegaEmbed` hook. Only change `spec` or `options` when a complete re-initialization of the visualization is desired.
affects: >=7.0.0
Errors
Common errors & fixes
Error: "spec.data" is not a function
Attempting to update data directly on the `spec` object passed to `VegaEmbed` or `useVegaEmbed` after initial render.
fix
Data 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')
Attempting to access the `view` property from the `useVegaEmbed` result before the embedding process is complete, when `result` is still `null`.
fix
Conditionally 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.
The `react-vega` package is not installed, or TypeScript cannot locate its declaration files (e.g., missing `@types/react` or misconfigured `tsconfig.json`).
fix
Ensure `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"`.
Upgrade
Version history
8.0.0latest on npm
Audit
Dependencies
reactrequiredPeer dependency required for any React application.
react-domrequiredPeer dependency required for rendering React components.
vega-embedrequiredCore library wrapped by react-vega; handles the actual embedding logic.
vega-literequiredRequired for rendering Vega-Lite specifications.
vegaoptionalRequired for rendering raw Vega specifications (if not using Vega-Lite).
Agent activity
4 hits · last 30 days
node
4
Resources
react-vega — npm install react-vega · libregistry