Skip to content

useReactFunctionComponents

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.

class Foo extends React.Component {
render() {
return (
<div>This is a class component.</div>
);
}
}
code-block.jsx:1:1 lint/nursery/useReactFunctionComponents ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Class components are not allowed. Function components are the preferred way to write components.

> 1 │ class Foo extends React.Component {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ render() {
> 3 │ return (
> 4 │ <div>This is a class component.</div>
> 5 │ );
> 6 │ }
> 7 │ }
^
8 │

Refactor this into a function component.

function Foo() {
return <div>This is a function component.</div>;
}
biome.json
{
"linter": {
"rules": {
"nursery": {
"useReactFunctionComponents": "error"
}
}
}
}