Dagre Layout

Beginner · 2 min read · Updated Nov 7, 2026

Dagre Layout: Automatic Node Positioning and Tree Layout

Dagre is a lightweight JavaScript library for automatically arranging directed graphs. It is a popular choice for React Flow applications that require clean, structured, and hierarchical node layouts.

If you're building workflow editors, flowcharts, organizational charts, or tree-based diagrams, Dagre provides a simple and efficient way to organize nodes without manually calculating their positions.

Why Use Dagre with React Flow?

Dagre focuses on generating fast and organized layouts for directed graphs. Its straightforward configuration makes it particularly useful for applications that need automatic node positioning and hierarchical structures.

Key Benefits of Dagre:

  • Automatic Node Positioning: Arrange nodes without manually defining coordinates.
  • Tree Layout Support: Create organized parent-child relationships and hierarchical diagrams.
  • Flexible Direction: Support left-to-right and top-to-bottom layouts.
  • Configurable Spacing: Adjust node separation, rank spacing, and graph alignment.
  • Fast Layout Calculation: Generate organized layouts with minimal configuration.

How Dagre Layout Works in React Flow

To integrate Dagre with React Flow, create a layout function such as getLayoutedElements. This function calculates node positions and returns updated nodes and edges.

The layout process generally involves four steps:

  1. Initialize a Dagre graph and configure its layout direction.
  2. Define the dimensions of each React Flow node.
  3. Add nodes and edges to Dagre's internal graph.
  4. Calculate the layout and update the original React Flow node positions.

This approach allows you to automatically arrange complex workflows while keeping your existing node and edge data intact.

Supporting Horizontal and Vertical Layouts

Dagre supports different graph directions through its rankdir configuration.

  • TB (Top to Bottom): Organizes nodes vertically.
  • LR (Left to Right): Arranges nodes horizontally.
  • BT (Bottom to Top): Creates an upward hierarchical layout.
  • RL (Right to Left): Arranges nodes from right to left.

By changing the layout direction dynamically, you can create flexible workflow editors that adapt to different diagram structures.

Customize Dagre Layout Spacing and Alignment

Dagre provides configuration options for controlling the distance between nodes and graph levels.

You can customize:

  • nodesep: Horizontal spacing between nodes in the same rank.
  • ranksep: Spacing between consecutive ranks.
  • edgesep: Spacing between edges.
  • rankdir: Overall graph direction.
  • align: Alignment of nodes within ranks.

These settings help improve diagram readability and prevent overcrowded workflow layouts.

When Should You Use Dagre?

Dagre is particularly suitable for React Flow applications that require straightforward hierarchical layouts, such as:

  • Tree diagrams and organizational charts
  • Workflow automation editors
  • Decision trees and flowcharts
  • Process management diagrams
  • Directed graph visualizations
  • Structured node-based applications

For developers who need a fast and simple automatic layout solution, Dagre is an excellent starting point.

Explore the Dagre layout configuration documentation to learn more about its available layout options.

Dagre LayoutOpen in New Tab