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

How to use forwardRef? #72

Open
albertorestifo opened this issue May 3, 2018 · 12 comments · May be fixed by #115 or #223
Open

How to use forwardRef? #72

albertorestifo opened this issue May 3, 2018 · 12 comments · May be fixed by #115 or #223

Comments

@albertorestifo
Copy link

What is the correct way to type a component which uses forwardRef form React 16.3?

@IOuser
Copy link

IOuser commented Aug 22, 2018

You can try to use this way:

export type ComponentProps = {
    foo?: boolean;
    bar: string;
    className?: string;
};


export const Component = React.forwardRef(
    (props: ComponentProps, ref?: React.Ref<HTMLButtonElement>) => {
        const { className, foo, bar } = props;

        return (
            <button className={className} ref={ref}>
                {foo ? bar : 'Buz'}
            </button>
        );
    },
);

@IssueHuntBot
Copy link

@BoostIO funded this issue with $20. Visit this issue on Issuehunt

1 similar comment
@IssueHuntBot
Copy link

@BoostIO funded this issue with $20. Visit this issue on Issuehunt

@IssueHuntBot
Copy link

@BoostIO cancelled funding, $20, of this issue. Visit this issue on Issuehunt

@IssueHuntBot
Copy link

@issuehuntfest has funded $20.00 to this issue. See it on IssueHunt

adrienharnay added a commit to adrienharnay/react-redux-typescript-guide that referenced this issue Dec 11, 2018
@adrienharnay adrienharnay linked a pull request Dec 11, 2018 that will close this issue
@IssueHuntBot
Copy link

@adrienharnay has submitted a pull request. See it on IssueHunt

@kyle-villeneuve
Copy link

kyle-villeneuve commented Dec 18, 2019

export const Component = React.forwardRef<HTMLButtonElement, ComponentProps>(
  (props, ref) => {
    return (
      <button {...props} ref={ref}>
         Submit
      </button>
    );
  }
);

@martisj
Copy link

martisj commented Mar 5, 2020

So @kyle-villeneuve do you have to deconstruct props to make it work?
Can't do this?

export const Component = React.forwardRef<HTMLButtonElement, ComponentProps>(
  (props, ref) => {
    return (
      <button ref={ref} {...props}>Stuff</button>
    );
  }
);

@sebastiandg7
Copy link

So @kyle-villeneuve do you have to deconstruct props to make it work?
Can't do this?

export const Component = React.forwardRef<HTMLButtonElement, ComponentProps>(
  (props, ref) => {
    return (
      <button ref={ref} {...props}>Stuff</button>
    );
  }
);

Yes @martisj, just make sure your ComponentProps interface extends React.HTMLProps<HTMLButtonElement>

@thainabiudes
Copy link

@IOuser great! thank u

@DedaDev
Copy link

DedaDev commented Aug 28, 2020

export const Component = React.forwardRef<HTMLButtonElement, ComponentProps>(
  (props, ref) => {
    return (
      <button {...props} ref={ref}>
         Submit
      </button>
    );
  }
);

Brooo
<3 <3 <3

@piotrwitek
Copy link
Owner

I'm proposing this updated version instead, which adds usage example and has a more dev friendly displayName in the DevTools:

export interface FancyButtonProps {
  className?: string;
  children?: React.ReactNode;
}

// using function DevTools will show "ForwardRef(FancyButton)" instead of just "ForwardRef"
export const FancyButton = React.forwardRef<HTMLButtonElement, FancyButtonProps>(
  function FancyButton(props, ref) {
    return (
      <button ref={ref} className="FancyButton" {...props} >
         {props.children}
      </button>
    );
  }
);

const ref = React.createRef<HTMLButtonElement>();
<FancyButton ref={ref}>Click me!</FancyButton>;

@piotrwitek piotrwitek linked a pull request May 3, 2022 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants