Skip to main content

API Reference

The library exports a single component, Graph, plus the TypeScript types you need to work with it:

import {
Graph,
type GraphType, // all props of <Graph />
type Node, // minimal node shape: { id: string | number }
type Link, // minimal link shape: { source: number; target: number }
type NodeType, // a node inside the simulation (x, y, vx, vy, ...)
type NodeComponentType, // signature for custom node renderers
type LinkComponentType, // signature for custom link renderers
} from "d3-graph-react";

Graph is generic over your node and link types: Graph<MyNode, MyLink> gives you full type-safety in NodeComponent and LinkComponent.

Props

PropTypeDefaultDescription
graph{ nodes: N[], links: L[] }requiredThe data to visualize. Links reference nodes by array index.
NodeComponentFC<{ node: N }>built-in boxRenders each node as your own React component.
LinkComponentFC<{ link, sourceNode, targetNode, … }>built-in lineRenders each link, with access to both endpoint positions and sizes.
zoomScale[number, number][0.5, 8]Minimum and maximum zoom factor.
onZoom(event: D3ZoomEvent) => voidCalled on every zoom or pan gesture.
linkForce{ strength, length }offPulls linked nodes toward a target distance.
gravityForce{ strength, center_x, center_y }offPulls the whole graph toward a center point.
chargeForce{ strength }offMakes nodes repel (negative) or attract (positive) each other.
isNodeDraggablebooleantrueEnables drag-and-drop on nodes.
ambientAlphaTargetnumber0Keeps the simulation gently running instead of settling.
onSimulationCreated(simulation) => voidHands you the raw d3-force simulation instance.
containerIdstringid applied to the container <div>.
containerClassNamestring"w-full flex"Class applied to the container <div>.
svgClassNamestring"w-full min-h-full"Class applied to the <svg> element.
Forces are opt-in

A force is only registered with the simulation when its prop is provided. Pass nothing and the nodes won't spread out — for a sensible layout, start with chargeForce, linkForce and gravityForce together (see the Quick Start).

isNodeDraggable

Dragging is enabled by default. While a node is dragged it is pinned to the cursor; on release it rejoins the simulation, which briefly reheats so the layout can adapt. Set isNodeDraggable={false} for a read-only graph:

<Graph graph={data} isNodeDraggable={false} />

Styling hooks

The component renders a <div> container wrapping an <svg>. Use these props to size and style them from your own CSS:

<Graph
graph={data}
containerId="org-chart"
containerClassName="my-graph-container"
svgClassName="my-graph-canvas"
/>
Give the container a height

The default classes make the graph fill its parent. If nothing shows up, the most common cause is a parent element with zero height — wrap the graph in a container with an explicit height (e.g. style={{ height: 500, display: "flex" }}).