ui: Export pattern causes warning from `eslint-plugin-react-refresh`

After initializing in a new React Vite TS Storybook starter, that happened to have this eslint plugin installed, I get warnings on a number of files that export functions in addition to a component.

Also, although documentation about “Fast Refresh” is a bit scant unfortunately, some resources on it also say that each export requires its own export statement…

https://github.com/ArnaudBarre/eslint-plugin-react-refresh

…from my eslint config:

{
  plugins: ['react-refresh'],
  rules: {
    'react-refresh/only-export-components': [
      'warn',
      { allowConstantExport: true },
    ],
  },
}

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 57
  • Comments: 18

Commits related to this issue

Most upvoted comments

Just came across this myself, maybe its worth considering re-opening?

bumping in 2025, still having this issue

same issue here, any updates?

Button, Sidebar, and Form components are affected by this issue. To prevent ESLint warnings, I’ve temporarily added a directive above the relevant code, as I don’t want to modify the entire shadcn component structure.

// eslint-disable-next-line react-refresh/only-export-components

I would also prefer that there were no warnings.

I’ve had the same issue with these components: badge button form sidebar And the theme-provider component.

Since I didn’t want to modify the components themselves (in order to keep my components less different than the original), I instead just add these lines to my eslint.config.js file:

  {
    files: ['**/ui/**/*.ts', '**/ui/**/*.tsx', '**/tools/theme-provider.tsx'],
    rules: {
      'react-refresh/only-export-components': 'off',
    },
  }

And here is my full eslint.config.js file for reference:

import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
  { ignores: ['dist'] },
  {
    extends: [js.configs.recommended, ...tseslint.configs.recommended],
    files: ['**/*.{ts,tsx}'],
    languageOptions: {
      ecmaVersion: 2020,
      globals: globals.browser,
    },
    plugins: {
      'react-hooks': reactHooks,
      'react-refresh': reactRefresh,
    },
    rules: {
      ...reactHooks.configs.recommended.rules,
      'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
    },
  },
  {
    files: ['**/ui/**/*.ts', '**/ui/**/*.tsx', '**/tools/theme-provider.tsx'],
    rules: {
      'react-refresh/only-export-components': 'off',
    },
  },
)

It was surprising that this hasn’t been a major issue, but it seems that most of the components provided by shadcn/ui are following the only-export-components rule. The only example of rule violation I found was with the button. Additionally, I don’t think the button needs to export the buttonVariants function. I have removed the buttonVariants function from the export object in the installed button.tsx.

Not ideal, but I had to restructure my project in order to fix it. Instead of having a single button.tsx file, I now have a

button folder with:

  • index.ts
  • button.tsx
  • variants.ts
# index.ts
export { button } from "./variants";
export { Button } from "./button";
export type { ButtonProps } from "./button";
# variants.ts
import { tv } from "tailwind-variants";

export const button = tv({
  base: "inline-flex justify-center items-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none disabled:pointer-events-none disabled:opacity-50",
  variants: {
    variant: {
      default: "bg-primary text-primary-foreground hover:bg-primary/90",
      destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
      outline: "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
      secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
      ghost: "hover:bg-accent hover:text-accent-foreground",
      link: "text-primary underline-offset-4 hover:underline",
    },
    ...
});
# button.tsx
import { Button as AriaButton, type ButtonProps as AriaButtonProps } from "react-aria-components";
import { button } from "./variants";

interface ButtonProps extends AriaButtonProps {
  className?: string;
  size?: "sm" | "lg" | "icon" | "field";
  variant?: "default" | "destructive" | "outline" | "secondary" | "ghost" | "link";
  focus?: "default" | "ring";
}

function Button({ children, className, size, variant, focus, ...props }: ButtonProps) {
  return (
    <AriaButton className={button({ size, variant, focus, className })} {...props}>
      {children}
    </AriaButton>
  );
}

export { Button };
export type { ButtonProps };

Same issue here in form when exporting useFormField

Adding the ESLint rule exception would hide the error, but it will still lead to Fast Refresh not working, which is not ideal. I have created a fresh issue #7736 since this one might not be reopened.

Also see the related issue I created in ArnaudBarre/eslint-plugin-react-refresh#84 which might throw some light on the root cause.

I came across the same issue in my project and had to disable the rule as @rafaltecza did.

The components I had the issue in were: badge button form sidebar

So, there is still no solution to this issue? Besides splitting the generated file ourselves?

I found out that button is required to export buttonVariants due to dependencies from components such as alert-dialog. To adhere to the only-export-components rule, I decided to define buttonVariables in a file other than button.tsx and import them into button.tsx.