0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Next.jsでReactFlowを使う

0
Last updated at Posted at 2025-08-05

Next.jsとは

Next.js is a React framework for building full-stack web applications. You use React Components to build user interfaces, and Next.js for additional features and optimizations.

Next.js公式ドキュメントより引用

つまりどういうこと?

Next.jsは、フルスタックWebアプリケーションを構築するためのReactフレームワーク。
Reactコンポーネントを使用してUIを構築しNext.jsによって機能を追加したり画像の最適化を行います。

ReactFlowとは

A customizable React component for building node-based editors and interactive diagrams

ReactFlow公式ドキュメントより引用

つまりどういうこと?

ReactFlowは、Reactでインタラクティブなフローチャートやダイアグラムを簡単に作成できるライブラリということです。

使用する環境

FW

  • Next.js(AppRouter)
  • ReactFlow (v12.8.2)
  • TypeScript

環境構築

プロジェクト作成とライブラリインストール

npx create-next-app@latest my-flow-app
cd my-flow-app
npm install @xyflow/react

基本的な使い方

シンプルなフローチャート

※注意 PCのテーマカラーをダークモードにしている場合、Next.jsがダークモードで開かれますが、ReactFlowはデフォルトでライトモードになっているため、文字が見えなくなることがあります。
その場合、後述するダークモードの設定を試してください

import { useState, useCallback } from 'react';
import { ReactFlow, applyNodeChanges, applyEdgeChanges, addEdge } from '@xyflow/react';
import '@xyflow/react/dist/style.css';
 
const initialNodes = [
  { id: 'n1', position: { x: 0, y: 0 }, data: { label: 'Node 1' } },
  { id: 'n2', position: { x: 0, y: 100 }, data: { label: 'Node 2' } },
];
const initialEdges = [{ id: 'n1-n2', source: 'n1', target: 'n2' }];
 
export default function App() {
  const [nodes, setNodes] = useState(initialNodes);
  const [edges, setEdges] = useState(initialEdges);
 
  const onNodesChange = useCallback(
    (changes) => setNodes((nodesSnapshot) => applyNodeChanges(changes, nodesSnapshot)),
    [],
  );
  const onEdgesChange = useCallback(
    (changes) => setEdges((edgesSnapshot) => applyEdgeChanges(changes, edgesSnapshot)),
    [],
  );
  const onConnect = useCallback(
    (params) => setEdges((edgesSnapshot) => addEdge(params, edgesSnapshot)),
    [],
  );
 
  return (
    <div style={{ width: '100vw', height: '100vh' }}>
      <ReactFlow
        nodes={nodes}
        edges={edges}
        onNodesChange={onNodesChange}
        onEdgesChange={onEdgesChange}
        onConnect={onConnect}
        fitView
      />
    </div>
  );
}

スクリーンショット 2025-08-05 221551.png

実用的な例

いろいろな機能の実装

実装している機能

  • パネルの表示
  • カラーテーマの変更
  • ミニマップの表示
  • ズームイン・ズームアウトの表示
// app/page.tsx
'use client';
import { useCallback, useState, type ChangeEventHandler } from 'react';
import {
  ReactFlow,
  addEdge,
  useNodesState,
  useEdgesState,
  MiniMap,
  Background,
  Controls,
  Panel,
  Position,
  type Node,
  type Edge,
  type ColorMode,
  type OnConnect,
} from '@xyflow/react';

import '@xyflow/react/dist/style.css';
import './page.css';

const nodeDefaults = {
  sourcePosition: Position.Right,
  targetPosition: Position.Left,
};

const initialNodes: Node[] = [
  {
    id: 'A',
    type: 'input',
    position: { x: 0, y: 150 },
    data: { label: 'A' },
    ...nodeDefaults,
  },
  {
    id: 'B',
    position: { x: 250, y: 0 },
    data: { label: 'B' },
    ...nodeDefaults,
  },
  {
    id: 'C',
    position: { x: 250, y: 150 },
    data: { label: 'C' },
    ...nodeDefaults,
  },
  {
    id: 'D',
    position: { x: 250, y: 300 },
    data: { label: 'D' },
    ...nodeDefaults,
  },
];

const initialEdges: Edge[] = [
  {
    id: 'A-B',
    source: 'A',
    target: 'B',
  },
  {
    id: 'A-D',
    source: 'A',
    target: 'D',
  },
];

export default function FlowChart() {
  const [colorMode, setColorMode] = useState<ColorMode>('dark');
  const [nodes, , onNodesChange] = useNodesState(initialNodes);
  const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);

  const onConnect: OnConnect = useCallback(
    (params) => setEdges((eds) => addEdge(params, eds)),
    [setEdges],
  );

  const onChange: ChangeEventHandler<HTMLSelectElement> = (evt) => {
    setColorMode(evt.target.value as ColorMode);
  };

  return (
    <div style={{ height: '500px', width: '100%' }} >
      <ReactFlow
        nodes={nodes}
        edges={edges}
        onNodesChange={onNodesChange}
        onEdgesChange={onEdgesChange}
        onConnect={onConnect}
        colorMode={colorMode}
        fitView
      >
        <MiniMap />
        <Background />
        <Controls />

        <Panel position="top-right">
          <select
            className="xy-theme__select"
            onChange={onChange}
            data-testid="colormode-select"
          >
            <option value="dark">dark</option>
            <option value="light">light</option>
            <option value="system">system</option>
          </select>
        </Panel>
      </ReactFlow>
    </div>
  );
};

スクリーンショット 2025-08-05 215706.png

実用例

ReactFlowは以下のような用途で活用できます:

  • 業務フローの可視化 - 承認プロセスや作業手順
  • システム構成図 - サーバーやサービスの関係
  • データフロー図 - データの流れや変換処理
  • 組織図 - 部署や人員の関係
  • マインドマップ - アイデアの整理

まとめ

ReactFlowを使えば、Next.jsアプリケーションに簡単にインタラクティブなフローチャート機能を追加できます。基本的な使い方から始めて、必要に応じてカスタムノードや高度な機能を追加していけば、ユーザーフレンドリーなダイアグラム作成ツールが作れるでしょう。

参考リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?