eslint-plugin-react

repository·master·Indexed 27 days ago

https://github.com/jsx-eslint/eslint-plugin-react

React-specific linting rules for ESLint to enforce best practices and catch common errors in React applications. Version 7.37.5 provides shareable configurations (recommended and all), support for the modern JSX runtime, and compatibility with both legacy .eslintrc and the new Flat Config system (eslint.config.js). It includes rules for React Server Actions, prop naming, button types, and destructuring assignments.

Tokens
54.2K
Snippets
127
Records
286
Agent score
92%

What's inside eslint-plugin-react

  1. Enforce defaultProps for non-required props with react/require-default-props

    master

    The react/require-default-props rule ensures that every prop not marked as required has a corresponding defaultProps definition. This rule works with PropTypes, TypeScript, or Flow definitions.

    Using defaultProps is preferred over custom default logic (like default function parameters) because React resolves defaultProps before PropTypes typechecking occurs, ensuring that typechecking applies to your default values.

    const HelloWorld = ({ name }) => (
      <h1>Hello, {name.first} {name.last}!</h1>
    );
    
    HelloWorld.propTypes = {
      name: PropTypes.shape({
        first: PropTypes.string,
        last: PropTypes.string,
      })
    };
    
    HelloWorld.defaultProps = {
      name: 'john'
    };
  2. Use the react/no-find-dom-node rule

    master

    The react/no-find-dom-node rule disallows the usage of findDOMNode. This is because findDOMNode is being deprecated by Facebook as it prevents certain future React improvements. This rule is enabled by default in the recommended configuration.

    To comply with this rule, use callback refs instead of findDOMNode to access DOM nodes.

    // Incorrect: Using findDOMNode
    class MyComponent extends Component {
      componentDidMount() {
        findDOMNode(this).scrollIntoView();
      }
      render() {
        return <div />
      }
    }
    
    // Correct: Using callback refs
    class MyComponent extends Component {
      componentDidMount() {
        this.node.scrollIntoView();
      }
      render() {
        return <div ref={node => this.node = node} />
      }
    }
  3. Use the react/async-server-action rule

    master

    The react/async-server-action rule ensures that any function containing the 'use server' directive is declared as async. This is required by the React Server Actions specification, even if the function body does not explicitly use await or return a Promise.

    This rule is manually fixable via editor suggestions.

    // Correct: Function is marked as async
    <form
      action={async () => {
        'use server';
        ...
      }}
    >
      ...
    </form>
    
    // Correct: Named function is marked as async
    async function action() {
      'use server';
      ...
    }
  4. Use the react/no-direct-mutation-state rule

    master

    The react/no-direct-mutation-state rule disallows direct mutation of this.state. You should treat this.state as immutable and use this.setState() to update state. Mutating this.state directly can cause subsequent calls to setState() to overwrite your changes.

    Exception: Assigning to this.state is only acceptable within an ES6 class component constructor during instance creation.

    // Correct: Using setState
    var Hello = createReactClass({
      componentDidMount: function() {
        this.setState({
          name: this.props.name.toUpperCase()
        });
      },
      render: function() {
        return <div>Hello {this.state.name}</div>;
      }
    });
    
    // Correct: Initializing state in constructor
    class Hello extends React.Component {
      constructor(props) {
        super(props);
    
        this.state = {
          foo: 'bar',
        };
      }
    }
  5. Disable react/react-in-jsx-scope for React 17+ JSX transform

    master

    If you are using the new JSX transform introduced in React 17, you should disable this rule by extending the plugin:react/jsx-runtime configuration in your ESLint config.

    Note: This rule is automatically disabled if React version 19.0.0 or higher is detected, as the automatic JSX transform is mandatory in React 19.

  6. Use the react/self-closing-comp rule

    master

    The react/self-closing-comp rule disallows extra closing tags for components that do not have children. This rule helps keep JSX clean by enforcing self-closing tags (e.g., <Component />) instead of explicit opening and closing pairs (e.g., <Component></Component>) when no content is present.

    This rule is automatically fixable using the ESLint --fix CLI option.

  7. Use the react/hook-use-state rule

    master

    The react/hook-use-state rule ensures that variables destructured from a React.useState() call follow the symmetric [thing, setThing] naming convention. It also prevents assigning the useState result to a single variable instead of destructuring it into a value and a setter pair.

    This rule provides editor suggestions to help fix naming issues manually.