Skip to content

Create "compound" component with forwardRef #3152

Answered by gamegee
gamegee asked this question in Q&A
Discussion options

You must be logged in to vote

I found a workaround, by forcing the type. I can go with this for now, it's clean

import { Box, ComponentWithAs, forwardRef } from '@chakra-ui/react'
import Item from './Item'

type Props = {
  menuType: 'primary' | 'secondary'
}

// defined type using chakra ComponentWithAs
type MenuCompoundType = ComponentWithAs<'div', Props> & {
  Item?: typeof Item
}


export const Menu: MenuCompoundType = forwardRef(({ children, ...restProps }, ref) => {
  // ... do my stuff
  return (
    <Box>{children}</Box>
  )
})

Menu.Item = Item // Works now !

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@Ryongyon
Comment options

@gamegee
Comment options

@alexalannunes
Comment options

Answer selected by dodas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants