Skip to content

Introduction to React Flow

React Flow is a library for building interactive node-based applications using React. It provides a simple API for creating flows, diagrams, and custom workflows. React Flow is especially useful for visualizing data relationships and building complex interfaces where users can manipulate nodes and edges.

Key Features of React Flow

  • Node and Edge Management: Easily manage nodes and edges with a straightforward API.
  • Customizable Components: Create custom nodes and edges to fit specific use cases.
  • Zoom and Pan: Supports zooming and panning, allowing users to navigate complex diagrams effortlessly.
  • Interactive: Provides drag-and-drop functionality for a more interactive user experience.
  • Flexible Layouts: Supports different layouts for nodes, enabling various design patterns.

Installation

To get started with React Flow in a Next.js application, you need to install the library:

npm install react-flow-renderer

Setting Up React Flow in a Next.js Application

Step 1: Create a New Component**

Create a new component (e.g., FlowChart.js) where you will set up the React Flow diagram.

// components/FlowChart.js
import React from 'react';
import ReactFlow, { MiniMap, Controls } from 'react-flow-renderer';

const initialNodes = [
  { id: '1', type: 'input', data: { label: 'Input Node' }, position: { x: 250, y: 5 } },
  { id: '2', data: { label: 'Default Node' }, position: { x: 100, y: 100 } },
  { id: '3', type: 'output', data: { label: 'Output Node' }, position: { x: 250, y: 200 } },
];

const initialEdges = [
  { id: 'e1-2', source: '1', target: '2' },
  { id: 'e2-3', source: '2', target: '3' },
];

const FlowChart = () => {
  return (
    <ReactFlow
      nodes={initialNodes}
      edges={initialEdges}
      style={{ width: '100%', height: '400px' }}
    >
      <MiniMap />
      <Controls />
    </ReactFlow>
  );
};

export default FlowChart;

Step 2: Create Custom Nodes

To create custom nodes, you can define your own React components and use them in the nodes array. Here’s an example of how to create a custom node:

// components/CustomNode.js
import React from 'react';

const CustomNode = ({ data }) => {
  return (
    <div style={{ padding: '10px', border: '1px solid #ddd', borderRadius: '5px', background: '#fff' }}>
      <strong>{data.label}</strong>
      <p>{data.description}</p>
    </div>
  );
};

export default CustomNode;

Step 3: Update FlowChart to Use Custom Nodes

Now, integrate the custom node into your FlowChart.js component:

import React from 'react';
import ReactFlow, { MiniMap, Controls } from 'react-flow-renderer';
import CustomNode from './CustomNode';

const initialNodes = [
  { id: '1', type: 'custom', data: { label: 'Custom Node', description: 'This is a custom node' }, position: { x: 250, y: 5 } },
  { id: '2', data: { label: 'Default Node' }, position: { x: 100, y: 100 } },
  { id: '3', type: 'output', data: { label: 'Output Node' }, position: { x: 250, y: 200 } },
];

const nodeTypes = {
  custom: CustomNode,
};

const FlowChart = () => {
  return (
    <ReactFlow
      nodes={initialNodes}
      edges={initialEdges}
      nodeTypes={nodeTypes}
      style={{ width: '100%', height: '400px' }}
    >
      <MiniMap />
      <Controls />
    </ReactFlow>
  );
};

export default FlowChart;

Step 4: Integrate the Flow Chart in a Next.js Page

Now, you can integrate the FlowChart component into a Next.js page.

// pages/index.js
import React from 'react';
import FlowChart from '../components/FlowChart';

const HomePage = () => {
  return (
    <div>
      <h1>My React Flow Diagram</h1>
      <FlowChart />
    </div>
  );
};

export default HomePage;

Step 5: Style Your Application

You can customize the appearance of your flow diagram using CSS. React Flow is highly customizable, allowing you to style nodes, edges, and the overall canvas.

/* styles/globals.css */
.react-flow__node-input {
  background: #fff;
  border: 1px solid #ddd;
}

.react-flow__node-default {
  background: #f0f0f0;
}

.react-flow__node-output {
  background: #e0e0e0;
}

/* Custom node styles */
.custom-node {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  background: #fff;
}

Conclusion

React Flow is a powerful tool for creating interactive diagrams in React applications. By integrating React Flow into a Next.js app and creating custom nodes, you can build visually appealing and user-friendly interfaces that allow for complex data visualizations. With its customizable components and easy-to-use API, React Flow enhances the user experience by providing a seamless way to manage and interact with data flows.