--- title: function-definition description: Validates and transforms React Client/Server Function definitions. --- This rule is experimental and may change in the future or be removed. It is not recommended for use in production code at this time. **Full Name in `eslint-plugin-react-rsc`** ```plain copy react-rsc/function-definition ``` **Full Name in [`@eslint-react/eslint-plugin`](https://npmx.dev/package/@eslint-react/eslint-plugin/v/latest)** ```plain copy @eslint-react/rsc-function-definition ``` **Features** `๐Ÿ”ง` `๐Ÿงช` **Presets** `rsc` `recommended` `recommended-typescript` `recommended-type-checked` `strict` `strict-typescript` `strict-type-checked` ## Rule Details It includes the following checks: 1. If a file has a top-level `'use server'` directive, exported functions must be async. Functions with their own `'use server'` directive must also be async. Note that this rule only checks exported functions; frameworks like Next.js additionally require that _all_ exports from a `'use server'` file be async functions, including non-function values. 2. If a file has a top-level `'use client'` or `'use server'` directive, it must be placed at the very beginning of the file, before any imports or other code. Comments above the directive are allowed. 3. If a function has a `'use server'` directive, it must be placed at the very beginning of the function body. 4. `'use client'` and `'use server'` directives must be written with single or double quotes, not backticks. 5. The `'use client'` directive must not be used inside a function body. It marks an entire module as client code, so it is only meaningful at the top of a file. ## Examples ### Server functions not marked as async ```tsx // ๐Ÿ”ด Problem: Exported server functions must be async 'use server'; export function serverFunction1() { // ... } export const serverFunction2 = function() { // ... }; export const serverFunction3 = () => { // ... }; ``` ```tsx // ๐Ÿ”ด Problem: Server functions inside components must be async export function Component() { function serverFunction1() { 'use server'; // ... } const serverFunction2 = function() { 'use server'; // ... }; const serverFunction3 = () => { 'use server'; // ... }; return
...
; } ``` ### Correctly marking server functions as async ```tsx // ๐ŸŸข Recommended: Exported async server functions 'use server'; export async function serverFunction1() { // ... } export const serverFunction2 = async function() { // ... }; export const serverFunction3 = async () => { // ... }; ``` ```tsx // ๐ŸŸข Recommended: Async server functions inside components export function Component() { async function serverFunction1() { 'use server'; // ... } const serverFunction2 = async function() { 'use server'; // ... }; const serverFunction3 = async () => { 'use server'; // ... }; return
...
; } ``` ### Incorrect directive placement or syntax ```tsx // ๐Ÿ”ด Problem: Directive must be at the very beginning of the file, before imports import { useState } from 'react'; 'use client'; ``` ```tsx // ๐Ÿ”ด Problem: Directive must be at the very beginning of the function body, before any other statements export function Component() { async function serverFunction() { const x = 1; 'use server'; // ... } } ``` ```tsx // ๐Ÿ”ด Problem: Directives must use single or double quotes, not backticks `use client`; ``` ```tsx // ๐Ÿ”ด Problem: 'use client' must not be used inside a function body export function Component() { function serverFunction() { 'use client'; // ... } } ``` ### Correct directive placement and syntax ```tsx // ๐ŸŸข Recommended: Directive at the top of the file before imports 'use client'; import { useState } from 'react'; ``` ```tsx // ๐ŸŸข Recommended: Directive at the beginning of the function body export function Component() { async function serverFunction() { 'use server'; // ... } } ``` ```tsx // ๐ŸŸข Recommended: Double quotes are also acceptable "use client"; import { useState } from 'react'; ``` ## Resources - [Rule Source](https://github.com/Rel1cx/eslint-react/tree/main/plugins/eslint-plugin-react-rsc/src/rules/function-definition/function-definition.ts) - [Test Source](https://github.com/Rel1cx/eslint-react/tree/main/plugins/eslint-plugin-react-rsc/src/rules/function-definition/function-definition.spec.ts) - [Rule Changelog](https://github.com/Rel1cx/eslint-react/tree/main/plugins/eslint-plugin-react-rsc/src/rules/function-definition/CHANGELOG.md) ## Further Reading - [React Docs: `'use client'` Caveats](https://react.dev/reference/rsc/use-client#caveats) - [React Docs: `'use server'` Caveats](https://react.dev/reference/rsc/use-server#caveats) - [Next.js Docs: Invalid "use server" Value](https://nextjs.org/docs/messages/invalid-use-server-value).