Skip to content

How to build custom components based on <Box>? #1418

Answered by hasparus
gregoralbrecht asked this question in Q&A
Discussion options

You must be logged in to vote

Okay, so one TypeScript error you can see is

Type 'RefObject<unknown>' is not assignable to type 'RefObject<HTMLDivElement>'.

Here's a CodeSandbox for you.
Ping me if you have any more problems :)



Sidenote: @theme-ui/components already exports Text. If you want to build your own primitive components you can ignore it entirely, and just use @theme-ui/core. You can build your own lightweight Box component with JSX pragma or manually using jsx imported from @theme-ui/core.


The root cause of the problem is the fact that React.forwardRef infers props: { children?: ReactNode }, ref: Ref<unknown>, so you assigned Ref<unknown> to Ref<HTMLDivElement>.

This doesn't work because.

  • Ref is covari…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@gregoralbrecht
Comment options

Comment options

You must be logged in to vote
4 replies
@gregoralbrecht
Comment options

@hasparus
Comment options

@hasparus
Comment options

@gregoralbrecht
Comment options

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