Skip to content

[QUESTION] How do you implement custom components with Typescript + forwardRef? #881

@calvinwyoung

Description

@calvinwyoung

I'm trying to implement a custom label component with Typescript, building on the <Label /> component that comes with theme-ui. My first attempt looks something like this:

export const CustomLabel = React.forwardRef<HTMLLabelElement, LabelProps>(
  (props , ref) => <Label ref={ref} {...props} />
);

I would've expected this to work, but instead I get the following error on ref={ref}:

Type 'string | ((instance: HTMLLabelElement | null) => void) | RefObject<HTMLLabelElement> | null' is not assignable to type '((instance: HTMLLabelElement | null) => void) | RefObject<HTMLLabelElement> | null | undefined'.
Type 'string' is not assignable to type '((instance: HTMLLabelElement | null) => void) | RefObject<HTMLLabelElement> | null | undefined'.

Am I doing something very silly here? Has anyone else run into a similar issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions