useReactFunctionComponents
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useReactFunctionComponents
- This rule doesn’t have a fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
Description
Section titled “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
Section titled “Examples”Invalid
Section titled “Invalid”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>;}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useReactFunctionComponents": "error" } } }}