React and Next.js
Find the React and Next.js issue types of fallow, which packages turn them on, and which React code fallow counts as used.
In a Next.js app, no file imports a page. Next.js loads each page.tsx from its place in the app/ folder, so fallow needs the Next.js plugin to know that the page is in use. With the plugin, fallow also finds mistakes that Next.js reports only in the build or at run time.
To see both, add next to the dependencies of the demo project, and add this page:
// app/orders/page.tsx
"use client";
import { formatPrice } from "../../src/lib";
export const metadata = { title: "Orders" };
export const pageSize = 20;
export default function OrdersPage() {
return <p>{formatPrice(1999)}</p>;
}
Without next in package.json, no plugin knows the page, and fallow dead-code --unused-files reports it:
● Unused files (2)
app/orders/page.tsx
src/utils/legacy-currency.ts
With next, the plugin makes the page an entry point, and the default export counts as used. The plugin also knows which other exports Next.js reads from a page, so fallow checks the rest:
4 entry points detected (2 package.json, 2 plugin)
● Unused exports (4)
app/orders/page.tsx
:7 pageSize
● Invalid client exports (1)
app/orders/page.tsx
:6 metadata (from "use client")
Server-only or route-config exports in a "use client" file (Next.js rejects
this at build time)
Next.js does not read pageSize, and no file imports it, so it is an unused export. metadata is a name that only a server file can export, so the build fails on this page.
Fallow reads the whole project, so it also finds React and Next.js problems that span files. Examples are two route files with one URL, a prop that goes through a chain of components, or a server action that no code calls. The sections below list these issue types. The rule page of each issue type has the example, the fix and the facts.
Each issue type runs only when the project declares the package that it needs. In a project without React or Next.js, these issue types report nothing.
Server Components directives
The "use client" and "use server" lines tell the bundler where a file runs. These issue types check how the files use these lines:
| Issue type | Finding | Runs when the project declares |
|---|---|---|
invalid-client-export | A "use client" file exports a name that only a server file can export, such as metadata or GET. The build fails. | next |
mixed-client-server-barrel | A barrel file re-exports a "use client" file and a server-only file. | next, waku, @lazarv/react-server, @vitejs/plugin-rsc or a react-server-dom-* package |
misplaced-directive | A "use client" or "use server" line comes after another statement, so the bundler ignores it. | next, waku, @lazarv/react-server, @vitejs/plugin-rsc or a react-server-dom-* package |
unused-server-action | No code calls an export of a "use server" file. | next |
mixed-client-server-barrel checks only the files that the barrel re-exports directly, and only project files. It skips a type-only re-export, and a re-export through a webpack inline loader such as export * from "raw-loader!./server", because the loader forwards its output and not the module.
An unused export of a "use server" file is an unused-server-action finding, not an unused-export finding. Its default severity is warn. To fail a run on it, set unused-server-action to error in rules.
App Router routes
The directives decide where code runs. The App Router also decides which file answers a URL, and two issue types check that each URL has one clear answer:
| Issue type | Finding | Default severity |
|---|---|---|
route-collision | Two or more route files resolve to the same URL. The build fails and names only one file. Fallow lists each file. | error |
dynamic-segment-name-conflict | Two dynamic segments at one position have different names, such as [id] and [slug]. The build passes, and the route fails on its first request. | error |
Both issue types run when the project declares next. Fallow checks each app separately, by workspace. Route groups (name) do not change the URL. Fallow leaves out private _folders and intercepting routes.
Components
The checks above look at files and routes. The next issue types look inside the components, at the props that flow between them. For React and Preact components, they run when the project declares react, react-dom, next or preact. unused-component-prop also checks Vue and Svelte components, and absent-component-prop checks the components of more frameworks. Their rule pages list each framework.
| Issue type | Finding | Default severity |
|---|---|---|
unused-component-prop | A component takes a prop from its first parameter and never reads it. | warn |
absent-component-prop | A component reads an optional prop, and no known caller passes it. | off |
prop-drilling | A prop goes through a chain of components that only pass it on. | off |
thin-wrapper | A component only renders one other component with all of its props. | off |
duplicate-prop-shape | Three or more components in two or more files declare the same prop names. | off |
For a React or Preact component, unused-component-prop checks the props that the first parameter destructures. It reports nothing for a component that is exported, because its props are part of a public contract. It also reports nothing for a component whose props it cannot list with certainty:
- a rest element, such as
{ ...rest }, or a rest parameter; - a props parameter without destructuring, as with
forwardReformemoover an imported interface; - an array pattern, a computed key, or a nested destructuring that fallow cannot flatten.
Four of these issue types are off by default. To turn one on, set it in your config file:
// .fallowrc.json
{
"rules": {
"prop-drilling": "warn",
"thin-wrapper": "warn",
"duplicate-prop-shape": "warn"
}
}
When prop-drilling is on, each chain also costs one point of the health score, at most five points.
Component render fan-in
The fan-in of a file counts the files that import it. A shared component such as <Button> is rendered in more places than it is imported. So, in a React or Preact project, fallow health also counts the parents that render each component. A parent is one component in one file, or the top level of a file. Fallow leaves out test, spec, story and fixture files.
Field of vital_signs | Content |
|---|---|
max_render_fan_in | The highest number of parents of one component. |
p95_render_fan_in | The 95th percentile of the number of parents. |
render_fan_in_high_pct | The percentage of components with more parents than the larger of p95_render_fan_in and 10. |
top_render_fan_in | The components with the most parents: component, path, distinct_parents, and render_sites (each render, also repeats in one parent). |
The human output prints the top five components on one Render fan-in: line. A complexity finding in a file that declares a component with many renders also has a blast radius: line. These numbers are context: they are no gate and no finding. Fallow counts only the renders that it can follow. It does not count a render through a JSX spread, through createElement with a variable, or through a member tag such as <Lib.Button />.
What fallow counts as used
The example at the top showed one convention: Next.js loads a page by its path. The plugins of fallow know how each router reaches code, so fallow does not report these files and exports as unused:
- Next.js App Router: the convention files (
page,layout,loading,error,routeand the metadata files), the route segment config,middleware,proxyandinstrumentation. - Next.js Pages Router: each file under
pages/, and the data functions that Next.js calls by name, such asgetStaticProps. - Server actions: an
action={fn}binding, a<form action={fn}>, an import and call, a prop that passes the action on, and a wrapped action factory. - React Router, Remix and TanStack Router: the route modules of each router.
When fallow reports a file that your router loads, first check that fallow list --plugins shows the plugin. Built-in plugins names the packages that turn each plugin on. When fallow misses a convention of your project, add the file to entry, or write a custom plugin.
Related
- Built-in plugins: check that the plugin of your router is on.
- Dead code and architecture issue types: the meaning of each finding.
- Issue types: each issue type of each command.