Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

refactor(vega): use useEffectEvent for VegaChart callbacks (#3735)

Cindy Zhang8da8975643f0
diff --git a/packages/vega/package.json b/packages/vega/package.json
index a23703f73d..7a6ab8968c 100644
--- a/packages/vega/package.json
+++ b/packages/vega/package.json
@@ -47,8 +47,8 @@
"typecheck": "tsc --noEmit"
},
"peerDependencies": {
- "react": ">=19.0.0",
- "react-dom": ">=19.0.0",
+ "react": ">=19.2.0",
+ "react-dom": ">=19.2.0",
"vega": ">=6.0.0",
"vega-lite": ">=6.0.0"
},
diff --git a/packages/vega/src/VegaChart.tsx b/packages/vega/src/VegaChart.tsx
index 7d3d9e3837..a031563e24 100644
--- a/packages/vega/src/VegaChart.tsx
+++ b/packages/vega/src/VegaChart.tsx
@@ -9,7 +9,7 @@
* SYNC: When modified, update /packages/vega/README.md
*/
-import React, {useEffect, useRef} from 'react';
+import React, {useEffect, useEffectEvent, useRef} from 'react';
import {parse, View} from 'vega';
import {compile} from 'vega-lite';
import {parseSchema} from './schema';
@@ -36,8 +36,9 @@ import type {VegaChartProps, VegaSpec, VegaLiteSpec} from './types';
* it when `spec`, `parseConfig`, `parseOptions`, or `viewOptions` changes,
* and calls `view.finalize()` on cleanup to release all runtime resources.
*
- * Callbacks (`onReady`, `onError`) are stable across renders via refs --
- * you don't need to memoize them. Pass stable references (or `useMemo`)
+ * Callbacks (`onReady`, `onError`) are non-reactive Effect Events -- they
+ * always see the latest props and never re-run the View lifecycle, so you
+ * don't need to memoize them. Pass stable references (or `useMemo`)
* for `parseConfig`, `parseOptions`, `viewOptions`, and `data` to avoid
* unnecessary re-renders.
*
@@ -80,17 +81,21 @@ export function VegaChart({
className,
style,
ref,
- onReady,
- onError,
+ onReady: onReadyProp,
+ onError: onErrorProp,
...props
}: VegaChartProps) {
const containerRef = useRef<HTMLDivElement>(null);
- // Keep callbacks in refs so they don't need to be in the dep array.
- const onReadyRef = useRef(onReady);
- const onErrorRef = useRef(onError);
- onReadyRef.current = onReady;
- onErrorRef.current = onError;
+ // The Effect fires these callbacks without treating them as reactive
+ // dependencies, so the View isn't torn down and rebuilt when a parent
+ // passes fresh inline callbacks on every render.
+ const onReady = useEffectEvent((view: View) => {
+ onReadyProp?.(view);
+ });
+ const onError = useEffectEvent((error: Error) => {
+ onErrorProp?.(error);
+ });
useEffect(() => {
const container = containerRef.current;
@@ -103,9 +108,7 @@ export function VegaChart({
const fail = (err: unknown) => {
if (!cancelled) {
- onErrorRef.current?.(
- err instanceof Error ? err : new Error(String(err)),
- );
+ onError(err instanceof Error ? err : new Error(String(err)));
}
};
@@ -149,7 +152,7 @@ export function VegaChart({
return;
}
if (view) {
- onReadyRef.current?.(view);
+ onReady(view);
}
})
.catch(fail);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index c74d561479..2429f300a9 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -755,10 +755,10 @@ importers:
packages/vega:
dependencies:
react:
- specifier: '>=19.0.0'
+ specifier: '>=19.2.0'
version: 19.2.7
react-dom:
- specifier: '>=19.0.0'
+ specifier: '>=19.2.0'
version: 19.2.7([email protected])
devDependencies:
'@types/react':