Sub Flow
React Flow supports rendering nested graphs and grouping of nodes. You can configure the behaviour of the child nodes using extent: 'parent'
and render group elements without handles by passing type: 'group'
to the node configuration. See our Sub Flow guide for more information.
import { useCallback } from 'react';
import {
ReactFlow,
addEdge,
Background,
useNodesState,
useEdgesState,
MiniMap,
Controls,
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
const initialNodes = [
{
id: '1',
type: 'input',
data: { label: 'Node 0' },
position: { x: 250, y: 5 },
},
{
id: '2',
data: { label: 'Group A' },
position: { x: 100, y: 100 },
style: { width: 200, height: 200 },
type: 'group',
},
{
id: '2a',
data: { label: 'Node A.1' },
position: { x: 10, y: 50 },
parentId: '2',
},
{
id: '3',
data: { label: 'Node 1' },
position: { x: 320, y: 100 },
},
{
id: '4',
data: { label: 'Group B' },
position: { x: 320, y: 200 },
style: { width: 300, height: 300 },
type: 'group',
},
{
id: '4a',
data: { label: 'Node B.1' },
position: { x: 15, y: 65 },
parentId: '4',
extent: 'parent',
},
{
id: '4b',
data: { label: 'Group B.A' },
position: { x: 15, y: 120 },
style: {
backgroundColor: 'rgba(255, 0, 255, 0.2)',
height: 150,
width: 270,
},
parentId: '4',
},
{
id: '4b1',
data: { label: 'Node B.A.1' },
position: { x: 20, y: 40 },
parentId: '4b',
},
{
id: '4b2',
data: { label: 'Node B.A.2' },
position: { x: 100, y: 100 },
parentId: '4b',
},
];
const initialEdges = [
{ id: 'e1-2', source: '1', target: '2', animated: true },
{ id: 'e1-3', source: '1', target: '3' },
{ id: 'e2a-4a', source: '2a', target: '4a' },
{ id: 'e3-4b', source: '3', target: '4b' },
{ id: 'e4a-4b1', source: '4a', target: '4b1' },
{ id: 'e4a-4b2', source: '4a', target: '4b2' },
{ id: 'e4b1-4b2', source: '4b1', target: '4b2' },
];
const NestedFlow = () => {
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = useCallback((connection) => {
setEdges((eds) => addEdge(connection, eds));
}, []);
return (
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
className="react-flow-subflows-example"
fitView
style={{ backgroundColor: "#F7F9FB" }}
>
<MiniMap />
<Controls />
<Background color='#E6E6E6' />
</ReactFlow>
);
};
export default NestedFlow;