Skip to main content
interlace
Plugin: react-featuresRules

jsx-max-depth

jsx-max-depth rule

Keywords: React, JSX nesting, component depth, complexity, refactoring, ESLint rule, LLM-optimized

Limits the maximum depth of JSX nesting to encourage component extraction and improve readability. This rule is part of eslint-plugin-react-features and provides LLM-optimized error messages.

Quick Summary

AspectDetails
SeverityWarning (code complexity)
Auto-FixโŒ No (requires component extraction)
CategoryReact
ESLint MCPโœ… Optimized for ESLint MCP integration
Best ForMaintaining readable components, encouraging composition

Rule Details

Why This Matters

IssueImpactSolution
๐Ÿ“– ReadabilityHard to follow nested JSXExtract to child components
๐Ÿงช TestabilityLarge components hard to testSmaller, focused components
๐Ÿ”„ ReusabilityNested JSX can't be reusedCreate reusable components
๐Ÿ› DebuggingDeeply nested errors hard to findIsolated component logic

Configuration

OptionTypeDefaultDescription
maxnumber5Maximum allowed JSX nesting depth

Examples

โŒ Incorrect (with max: 3)

Awaiting a tested example. The previous snippet was removed because the rule does not behave as the doc claimed; track the regression in benchmarks/FP_FN_REMEDIATION_TRACKER.md.

โœ… Correct (with max: 3)

// Extracted component reduces depth
function ArticleContent() {
  return <p>Content</p>;
}

function Article() {
  return (
    <article>
      <ArticleContent />
    </article>
  );
}

function ShallowComponent() {
  return (
    <div>                    {/* depth 1 */}
      <section>              {/* depth 2 */}
        <Article />          {/* depth 3 - component boundary */}
      </section>
    </div>
  );
}

Configuration Examples

Default (max: 5)

{
  rules: {
    'react-features/jsx-max-depth': 'warn'
  }
}

Strict (max: 3)

{
  rules: {
    'react-features/jsx-max-depth': ['error', { max: 3 }]
  }
}

Lenient (max: 7)

{
  rules: {
    'react-features/jsx-max-depth': ['warn', { max: 7 }]
  }
}

Refactoring Strategy

Depth LevelRecommended Action
1-3โœ… Good - readable structure
4-5โš ๏ธ Consider extracting reusable parts
6+๐Ÿ”ด Extract into child components

Before Refactoring

function Dashboard() {
  return (
    <div className="dashboard">
      <header>
        <nav>
          <ul>
            <li>
              <a href="/">Home</a>  {/* depth 5 */}
            </li>
          </ul>
        </nav>
      </header>
    </div>
  );
}

After Refactoring

function NavLink({ href, children }) {
  return <li><a href={href}>{children}</a></li>;
}

function Navigation() {
  return (
    <nav>
      <ul>
        <NavLink href="/">Home</NavLink>
      </ul>
    </nav>
  );
}

function Dashboard() {
  return (
    <div className="dashboard">
      <header>
        <Navigation />
      </header>
    </div>
  );
}

Further Reading

Known False Negatives

The following patterns are not detected due to static analysis limitations:

Dynamic Variable References

Why: Static analysis cannot trace values stored in variables or passed through function parameters.

// โŒ NOT DETECTED - Prop from variable
const propValue = computedValue;
<Component prop={propValue} /> // Computation not analyzed

Mitigation: Implement runtime validation and review code manually. Consider using TypeScript branded types for validated inputs.

Imported Values

Why: When values come from imports, the rule cannot analyze their origin or construction.

// โŒ NOT DETECTED - Value from import
import { getValue } from './helpers';
processValue(getValue()); // Cross-file not tracked

Mitigation: Ensure imported values follow the same constraints. Use TypeScript for type safety.

Did this rule catch something? Star the repo to get new CWE coverage as we ship it โ€” or follow the AI-code-security benchmarks behind these rules.