Back to Biomejs

useReactFunctionComponents

src/content/docs/linter/rules/use-react-function-components.mdx

latest5.6 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> ## Summary - Rule available since: `v2.1.3` - Diagnostic Category: [`lint/style/useReactFunctionComponents`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - This rule belongs to the following domains: - [`react`](/linter/domains#react) - Sources: - Same as [`react-prefer-function-component/react-prefer-function-component`](https://github.com/tatethurston/eslint-plugin-react-prefer-function-component)

How to configure

json
{
	"linter": {
		"rules": {
			"style": {
				"useReactFunctionComponents": "error"
			}
		}
	}
}

Description

Enforce that components are defined as functions and never as classes.

React in particular allows users to create components using functions or classes. However, using functions is generally preferred. This rule enforces the use of function components.

This rule makes an exception for class components that implement componentDidCatch because there is currently no hook alternative for React. This function is typically used for defining error boundaries. It's recommended to define your error boundary once and then reuse it across your application.

If you are using Preact, it has a useErrorBoundary hook.

Examples

Invalid

jsx
class Foo extends React.Component {
  render() {
    return (
      <div>This is a class component.</div>
    );
  }
}
<pre class="language-text"><code class="language-text">code-block.jsx:1:1 <a href="https://biomejs.dev/linter/rules/use-react-function-components">lint/style/useReactFunctionComponents</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Class components are not allowed. Function components are the preferred way to write components.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>class Foo extends React.Component &#123; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> render() &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> return ( <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> &lt;div&gt;This is a class component.&lt;/div&gt; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>5 │ </strong> ); <strong><span style="color: Tomato;">&gt;</span></strong> <strong>6 │ </strong> &#125; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>7 │ </strong>&#125; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong> <strong>8 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Refactor this into a function component.</span> </code></pre>

Valid

jsx
function Foo() {
  return <div>This is a function component.</div>;
}
</TabItem> </Tabs>