React Flow
React Flow is a highly customizable React library for building interactive graphs, node-based editors, and complex flow charts. The package `reactflow` is currently at version 11.11.4, which represents the actively maintained v11 stable release line. The project maintains a consistent release cadence with frequent patch updates and less frequent minor/major releases. Key differentiators include its intuitive API for seamless zooming and panning, extensive customization options for both nodes and edges, performant rendering achieved by only re-rendering changed elements, and a rich set of hooks and utility functions for graph manipulation. It also provides essential plugin components like MiniMap, Controls, and Background out-of-the-box. The library is built with TypeScript and tested with Cypress, ensuring reliability and a robust developer experience. Users should note that the upcoming v12 release will transition to a new package name, `@xyflow/react`.
Common errors
-
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause React Flow hooks like `useNodesState` or `useEdgesState` are being called outside of a functional React component, or there are multiple instances of React loaded.fixEnsure all React Flow hooks are called within the top level of a functional React component. If using multiple React versions, ensure your bundler resolves to a single React instance. -
TypeError: Cannot read properties of undefined (reading 'x') (or similar rendering issues like nodes overlapping/not appearing)
cause The `ReactFlow` component's container does not have explicit `width` and `height` CSS properties, or the `reactflow/dist/style.css` stylesheet is not imported.fixProvide a defined `width` and `height` to the `div` wrapper around your `ReactFlow` component (e.g., `style={{ width: '100vw', height: '100vh' }}`). Additionally, verify that `import 'reactflow/dist/style.css';` is present in your application. -
Minimap is not defined / Controls is not defined / Background is not defined
cause One of the optional UI components (MiniMap, Controls, Background) is used but not imported from `reactflow`.fixEnsure all desired components are explicitly imported using named imports: `import { MiniMap, Controls, Background } from 'reactflow';`
Warnings
- breaking The `reactflow` package (v11) is being succeeded by `@xyflow/react` (v12). This transition involves a package name change and significant API updates. Users of `reactflow` should plan for a migration when moving to v12.
- gotcha Forgetting to import the `reactflow/dist/style.css` stylesheet will result in an unstyled and potentially non-functional React Flow canvas, as essential layout and component styles are missing.
- gotcha React Flow requires `react` and `react-dom` as peer dependencies with versions `>=17`. Using older versions of React may lead to compatibility issues or errors.
- gotcha When implementing custom nodes or edges, they must be explicitly registered with the `nodeTypes` and `edgeTypes` props on the `ReactFlow` component. Failing to do so will prevent your custom elements from rendering.
Install
-
npm install reactflow -
yarn add reactflow -
pnpm add reactflow
Imports
- ReactFlow
import { ReactFlow } from 'reactflow';import ReactFlow from 'reactflow';
- useNodesState
import useNodesState from 'reactflow';
import { useNodesState } from 'reactflow'; - MiniMap
import MiniMap from 'reactflow';
import { MiniMap, Controls, Background } from 'reactflow'; - StyleSheet
import 'reactflow/style.css';
import 'reactflow/dist/style.css';
Quickstart
import { useCallback } from 'react';
import ReactFlow, {
MiniMap,
Controls,
Background,
useNodesState,
useEdgesState,
addEdge,
} from 'reactflow';
import 'reactflow/dist/style.css';
const initialNodes = [
{ id: '1', position: { x: 0, y: 0 }, data: { label: 'Node 1' } },
{ id: '2', position: { x: 250, y: 150 }, data: { label: 'Node 2' } },
{ id: '3', position: { x: 100, y: 250 }, data: { label: 'Node 3' } },
];
const initialEdges = [
{ id: 'e1-2', source: '1', target: '2', label: 'to the next' },
{ id: 'e2-3', source: '2', target: '3', animated: true },
];
function Flow() {
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = useCallback((params) => setEdges((eds) => addEdge(params, eds)), [setEdges]);
return (
<div style={{ width: '100vw', height: '100vh' }}>
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
fitView
>
<MiniMap />
<Controls />
<Background variant="dots" gap={12} size={1} />
</ReactFlow>
</div>
);
}
export default Flow;