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.
npm install eslint-plugin-react-server-componentsVerified import paths — ran on the pinned version, not inferred.
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.
Refactor existing class components to functional components, or ensure they are explicitly marked with `"use client"` if intended for the client bundle and interact with client-only features.
Always place the `"use client"` directive at the very top of the file, before any imports or other code, as the first non-comment statement. Refer to the official React documentation for exact placement rules.
Always ensure your `.eslintrc` configuration includes `"extends": ["plugin:react-server-components/recommended"]` and that `eslint-plugin-react-server-components` is correctly installed as a dev dependency.
Ensure your ESLint configuration includes a parser like `@babel/eslint-parser` or `@typescript-eslint/parser` and appropriate `ecmaVersion` (`'latest'`) and `sourceType` (`'module'`) settings to support JSX and ES modules.
Add `"use client"` as the very first line of the file for components that utilize client-only features. Alternatively, if it's meant to be a server component, remove any client-only hooks or APIs from it.
Double-check the `allowedServerHooks` option name for correct capitalization and ensure it is provided as an array within an object, which is then nested as the second element in the rule's configuration array (e.g., `['error', { 'allowedServerHooks': ['myCustomHook'] }]`).