logoESLint React
Rules

no-use-in-try-catch

Full Name in eslint-plugin-react-hooks-extra

react-hooks-extra/no-use-in-try-catch

Full Name in @eslint-react/eslint-plugin

@eslint-react/hooks-extra/no-use-in-try-catch

Features

🔍

Presets

  • recommended
  • recommended-typescript
  • recommended-type-checked

What it does

This rule disallows the use of use in a try-catch block.

use cannot be called in a try-catch block. Instead of a try-catch block wrap your component in an Error Boundary, or provide an alternative value to use with the Promise’s .catch method.

Examples

Failing

function Message({ messagePromise }) {
  try {
    const content = use(messagePromise);
    //              ^^^
    //              - 'use' cannot be called in a try-catch block. Instead of a try-catch block wrap your component in an Error Boundary, or provide an alternative value to use with the Promise’s .catch method.
    return <p>Here is the message: {content}</p>;
  } catch (error) {
    return <p>⚠️Something went wrong</p>;
  }
}

Implementation

Further Reading

On this page