Babel Plugin for Next.js 'use client' Directive

1.1.1 · active · verified Tue Apr 21

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.

Common errors

Warnings

Install

Imports

Quickstart

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`.

{
  // In your babel.config.js or .babelrc file
  "presets": [
    // ... other presets like '@babel/preset-env', '@babel/preset-react', 'next/babel'
  ],
  "plugins": [
    // ... other plugins
    'babel-plugin-transform-next-use-client',
    // If you have custom client-only hooks or modules, specify them:
    // [
    //   'babel-plugin-transform-next-use-client',
    //   {
    //     customClientImports: [
    //       'useAuthClientStore', // Example: a custom hook that uses browser APIs
    //       'myClientSideUtility'
    //     ]
    //   }
    // ]
  ]
}

// Example React component that would trigger the plugin:
// components/MyClientComponent.tsx
// (No need for manual 'use client' here if plugin is active)

import React, { useState, useEffect } from 'react';

interface MyClientComponentProps {
  initialCount?: number;
}

export default function MyClientComponent({ initialCount = 0 }: MyClientComponentProps) {
  const [count, setCount] = useState(initialCount);

  useEffect(() => {
    // This effect runs only on the client
    console.log('Component mounted on client');
    document.title = `Count: ${count}`;
    return () => {
      console.log('Component unmounted from client');
    };
  }, [count]);

  return (
    <div>
      <h1>Client Component Example</h1>
      <p>Current count: {count}</p>
      <button onClick={() => setCount(prev => prev + 1)}>Increment</button>
      <p>This component uses `useState` and `useEffect`, triggering the 'use client' directive automatically.</p>
    </div>
  );
}

view raw JSON →