Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export pattern causes warning from eslint-plugin-react-refresh #1534

Closed
lunelson opened this issue Sep 16, 2023 · 15 comments
Closed

Export pattern causes warning from eslint-plugin-react-refresh #1534

lunelson opened this issue Sep 16, 2023 · 15 comments
Labels

Comments

@lunelson
Copy link

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 },
    ],
  },
}
@enix79
Copy link

enix79 commented Oct 13, 2023

same issue here, any updates?

@shadcn shadcn added the Stale label Feb 14, 2024
@DocMoebiuz
Copy link

I would also prefer that there were no warnings.

@koutaro-masaki
Copy link

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.

ilyakam added a commit to ilyakam/catwalk.chat that referenced this issue May 1, 2024
Update the codebase to match my preferred style using `stylelint`,
`eslint`, and `.editorconfig`. Run ESLint, Stylelint, and TypeScript
in parallel on `pnpm run lint` with `npm-run-all`. Ignore the resulting
cache files (`.eslintcache` and `.stylelintcache`).

Remove the warning for allowing only constant exports in order to have
shadcn/ui continue to export `buttonVariants` without throwing an error
(see shadcn-ui/ui#1534).

Alphabetically sort the configuration and code where possible (:abc:).
ilyakam added a commit to ilyakam/catwalk.chat that referenced this issue May 1, 2024
Update the codebase to match my preferred style using `stylelint`,
`eslint`, and `.editorconfig`. Run ESLint, Stylelint, and TypeScript
in parallel on `pnpm run lint` with `npm-run-all`. Ignore the resulting
cache files (`.eslintcache` and `.stylelintcache`).

Remove the warning for allowing only constant exports in order to have
shadcn/ui continue to export `buttonVariants` without throwing an error
(see shadcn-ui/ui#1534).

Alphabetically sort the configuration and code where possible (:abc:).
ilyakam added a commit to ilyakam/catwalk.chat that referenced this issue May 1, 2024
Update the codebase to match my preferred style using `stylelint`,
`eslint`, and `.editorconfig`. Run ESLint, Stylelint, and TypeScript
in parallel on `pnpm run lint` with `npm-run-all`. Ignore the resulting
cache files (`.eslintcache` and `.stylelintcache`).

Remove the warning for allowing only constant exports in order to have
shadcn/ui continue to export `buttonVariants` without throwing an error
(see shadcn-ui/ui#1534).

Alphabetically sort the configuration and code where possible (:abc:).
@koutaro-masaki
Copy link

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.

@shadcn shadcn added the Stale label Jun 18, 2024
@shadcn
Copy link
Collaborator

shadcn commented Jun 25, 2024

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

@shadcn shadcn closed this as completed Jun 25, 2024
@philbates35
Copy link

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

@GuillemPM
Copy link

Same issue here in form when exporting useFormField

@jay-batavia
Copy link

Same issue here in form when exporting useFormField

@GuillemPM For my use, I removed the useFormField as an export from the file. It is only being used internally by the other Form components. So unless you are using the useFormField hook somewhere in your code, removing it from the export list should solve the issue.

@dmccoy-NL
Copy link

Would be great if this was looked at again.

@elalgarro
Copy link

elalgarro commented Sep 28, 2024

This warning also appears when copypasting the theme provider example into a fresh create-vite project. Same workaround that @koutaro-masaki mentions works. Move the hook into its own file.

@illustrofia
Copy link

What does one have to do to get a issue reopened around here, btw? I love this project, that's why I'd like for it to be better managed 🤓

@denisorehovsky
Copy link

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 };

@augnustin
Copy link

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

@majicmaj
Copy link

bumping in 2025, still having this issue

@rafaltecza
Copy link

rafaltecza commented Feb 28, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests