The `babel-plugin-transform-next-use-client` is a specialized Babel plugin designed to automatically inject the "use client" directive into React components within a Next.js application. It identifies components that utilize React client-only APIs, such as `useEffect` and `useState`, and programmatically adds the directive, ensuring these components are correctly designated for client-side rendering according to Next.js App Router conventions. This automation helps prevent common errors where client-specific code might accidentally be rendered on the server. The current stable version is 1.1.1. As a utility within the build toolchain, its release cadence is generally stable, with updates typically driven by significant changes in React or Next.js's component model. Its key differentiator is simplifying the management of client components, reducing manual boilerplate and ensuring proper separation of client and server code within the App Router paradigm. It also provides an option (`customClientImports`) to specify custom client-only hooks or modules for accurate detection.
npm install babel-plugin-transform-next-use-clientVerified import paths — ran on the pinned version, not inferred.
Demonstrates the basic setup of the plugin in a Babel configuration and an example React component that would be automatically marked as a 'use client' component due to its use of `useState` and `useEffect`.
Ensure your project is a Next.js App Router application. If not, this plugin is unnecessary and should be removed.
Use the `customClientImports` option in your Babel configuration to explicitly list the names of your custom client-only hooks or functions. For example: `[['babel-plugin-transform-next-use-client', { customClientImports: ['useMyHook'] }]]`.Review your `plugins` array in `babel.config.js`. Generally, this plugin should be placed early in the `plugins` array to ensure it processes the code before other major transformations occur.
Verify your `babel.config.js` includes the plugin. If you're using custom client-only hooks or modules, ensure they are listed in the `customClientImports` option of the plugin configuration.
Run `npm install babel-plugin-transform-next-use-client` or `yarn add babel-plugin-transform-next-use-client`. Double-check the spelling in your `babel.config.js`.