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

Possible false positive/negative #35

Closed
innerdaze opened this issue Dec 19, 2023 · 1 comment
Closed

Possible false positive/negative #35

innerdaze opened this issue Dec 19, 2023 · 1 comment

Comments

@innerdaze
Copy link

Given the following code...

// --> I see an error here <--
const MenuItemLinkRoot = styled(MenuItem, {
  name: 'MenuItemLink',
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root,
})<MenuItemLinkProps>``

const useUtilityClasses = (ownerState: Partial<MenuItemLinkProps>) => {
  const slots = {
    root: ['root'],
  }

  return unstable_composeClasses(slots, getMenuItemLinkUtilityClass, ownerState.classes)
}


// --> and here <--
const MenuItemLink = (props: MenuItemLinkProps) => {
  const { className, classes, ...rootProps } = props

  const slotClasses = useUtilityClasses({ classes })

  const routeMatch = useRouteMatch(props.to as string)

  return (
    <MenuItemLinkRoot
      {...rootProps}
      className={classnames(slotClasses.root, className)}
      selected={props.to === routeMatch?.path}
    />
  )
}

const _MenuItemLink = React.memo(MenuItemLink) as typeof MenuItemLink

export default _MenuItemLink

So as marked by the comments above, I see the error Fast refresh only works when a file only exports components. Move your component(s) to a separate file.

As I am only exporting one component from this file, this seem incorrect.

I would guess this is to do with the leading _ on the default export.

If I change the code so that: -

  • MenuItemLink becomes _MenuItemLink
  • _MenuItemLink becomes MenuItemLink

(so that the exported name starts with a capital, and the non-exported component startds with a _)

then it works without error.

@ArnaudBarre
Copy link
Owner

Yeah this is an assumption builtin in the React refresh runtime: a function should start with a capital letter to be considered a component: https://github.com/facebook/react/blob/main/packages/react-refresh/src/ReactFreshRuntime.js#L715

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

No branches or pull requests

2 participants