UI frameworks
React
Mount a Vela Pro workspace in a React component, update chart state from props, and clean up safely under Strict Mode.
Vela's open-source core and Vela Pro have no UI framework dependencies and render directly inside a DOM container element. In React, call enableVelaPro() once at module scope, construct VelaWorkspace inside useEffect when the container mounts, and call workspace.destroy() when the component unmounts.
Component setup
import { useEffect, useRef } from 'react';
import { VelaWorkspace } from '@luxalgo/vela/workspace';
import { BinanceProvider } from '@luxalgo/vela/providers/binance';
import { enableVelaPro } from '@luxalgo/vela-pro';
enableVelaPro();
export function Chart({ symbol }: { symbol: string }) {
const containerRef = useRef<HTMLDivElement>(null);
const workspaceRef = useRef<VelaWorkspace | null>(null);
useEffect(() => {
const workspace = new VelaWorkspace(containerRef.current!, {
layout: false,
symbol,
timeframe: '60',
live: true,
providers: { binance: () => new BinanceProvider() },
});
workspaceRef.current = workspace;
return () => {
workspaceRef.current = null;
workspace.destroy();
};
}, []);
useEffect(() => {
const workspace = workspaceRef.current;
if (workspace && workspace.active.symbol !== symbol) {
workspace.active.setSymbol(symbol);
}
}, [symbol]);
return <div ref={containerRef} style={{ width: '100%', height: '100%' }} />;
}Lifecycle and state rules
- Initialize at module scope: Place
enableVelaPro()outside the component function (or in your application entry file) so it executes once when the module loads and before the firstVelaWorkspaceinstance is constructed, regardless of how many<Chart />components mount. - Update from props without recreating the workspace: Keep the mount
useEffectdependency array empty ([]). RecreatingVelaWorkspaceon prop changes tears down the chart and discards active drawings, indicators, and panel state. Push prop updates in place throughworkspace.active.setSymbol(symbol),workspace.active.setTimeframe(timeframe), orworkspace.setTheme(theme). - Clean up for React Strict Mode: React Strict Mode mounts, unmounts, and remounts components in development. Returning
workspace.destroy()from the mount effect flushes pending persistence writes, stops Web Workers and provider subscriptions, and clears the container DOM so remounting leaves a single workspace instance. - Size the container element:
VelaWorkspacefills the dimensions of its mount element. Give the parent container an explicit height (for exampleheight: '100vh'or a flex/grid container withmin-height: 0).
Calling imperative methods
Store the VelaWorkspace instance in workspaceRef when parent components or event handlers need imperative access to the workspace or active chart:
// Save the current layout and chart state to your backend
const snapshot = workspaceRef.current?.getState();
// Sync the workspace with an application theme toggle
workspaceRef.current?.setTheme('light');
// Add a Pine Script® indicator to the active chart
workspaceRef.current?.chart.addIndicator(
'//@version=6\nindicator("EMA 20", overlay=true)\nplot(ta.ema(close, 20))',
);Verified environment
| Stack | Verified versions | Checks run |
|---|---|---|
| React + Vite | React 19.3, Vite 8.3 | Development server with React Strict Mode enabled and production build (vite build) |