ESLint Plugin for React Server Components

1.2.0 · active · verified Sun Apr 19

eslint-plugin-react-server-components is an ESLint plugin providing rules specifically designed to manage and enforce the correct usage of React Server Components (RSCs) and Client Components. As of version 1.2.0, this plugin helps developers identify components that incorrectly mix server-side and client-side logic, primarily by ensuring that components using client-only features (like `useState`, `useEffect`, or browser APIs) are correctly prefixed with the `"use client"` directive. It also helps detect instances where `"use client"` might be unnecessarily applied. The plugin features a `recommended` configuration for easy setup and offers options like `allowedServerHooks` (introduced in v1.2.0) to whitelist specific hooks that should not trigger errors in server components. With regular patch and minor releases, the project appears to be actively maintained, adapting to the evolving best practices for RSCs, particularly relevant for frameworks like Next.js that heavily utilize this paradigm. Its key differentiation lies in its focused approach to linting the specific contract between server and client components.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a React component that uses client-side hooks (`useState`) without the necessary `"use client"` directive. This code, when linted with the plugin's recommended configuration, will trigger an error from the `react-server-components/use-client` rule, enforcing proper component type declaration for React Server Components.

import React from 'react';

// This component uses client-side state without the 'use client' directive.
// This ESLint plugin would flag this as an error under its recommended ruleset.
function MyClientComponent() {
  const [count, setCount] = React.useState(0);
  
  return (
    <button onClick={() => setCount(count + 1)}>
      Count: {count}
    </button>
  );
}

export default MyClientComponent;

// To lint this file, you would run:
// npm install --save-dev eslint eslint-plugin-react-server-components
// Create an .eslintrc.json file:
// {
//   "parserOptions": { "ecmaVersion": "latest", "sourceType": "module" },
//   "extends": ["plugin:react-server-components/recommended"]
// }
// Then run: npx eslint myClientComponent.jsx

view raw JSON →