Forwarding React Refs with TypeScript | Building SPAs

PHOTO EMBED

Wed Sep 15 2021 08:27:26 GMT+0000 (UTC)

Saved by @chillymood #typescript

const Component = React.forwardRef<RefType, PropsType>((props, ref) => {
  return someComponent;
});
content_copyCOPY

forwardRef is a generic function that has type parameters for the type of the ref and the props:

https://www.carlrippon.com/react-forwardref-typescript/