Getting Started -> Adding Interactivity -> Handle Connections: use setEdges function and addEdge function to handle interactivity that connecting nodes manually

master
Jason Zhu 2023-05-29 20:08:37 +10:00
parent 328f950ea6
commit f06509e904
1 changed files with 8 additions and 3 deletions

View File

@ -4,14 +4,13 @@ import ReactFlow, {
Controls, Controls,
applyEdgeChanges, applyEdgeChanges,
applyNodeChanges, applyNodeChanges,
addEdge,
} from 'reactflow'; } from 'reactflow';
// we have to import the React Flow styles for it to work // we have to import the React Flow styles for it to work
import 'reactflow/dist/style.css'; import 'reactflow/dist/style.css';
const initialEdges = [ const initialEdges = [];
{ id: '1-2', source: '1', target: '2', label: 'to the', type: 'step' },
];
const initialNodes = [ const initialNodes = [
{ {
@ -40,6 +39,11 @@ const Flow = () => {
[], [],
); );
const onConnect = useCallback(
params => setEdges(eds => addEdge(params, eds)),
[],
);
return ( return (
<div style={{ height: '100vh' }}> <div style={{ height: '100vh' }}>
<ReactFlow <ReactFlow
@ -47,6 +51,7 @@ const Flow = () => {
onNodesChange={onNodesChange} onNodesChange={onNodesChange}
edges={edges} edges={edges}
onEdgesChange={onEdgesChange} onEdgesChange={onEdgesChange}
onConnect={onConnect}
> >
<Background /> <Background />
<Controls /> <Controls />