Skip to content

Instantly share code, notes, and snippets.

@HughParsons
Created September 9, 2022 00:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HughParsons/f90142528e498d50e84876a9f550a5ad to your computer and use it in GitHub Desktop.
Save HughParsons/f90142528e498d50e84876a9f550a5ad to your computer and use it in GitHub Desktop.
React HOC (higher order component) with forwardRef
import * as React from 'react'
export function HOC<T, P extends {}>(
Component: React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<T>>
): React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<T>> {
return React.forwardRef<T, P>(
function ComponentFromHOC(props, ref) {
return (
<Component {...props as React.PropsWithoutRef<P>} ref={ref} />
);
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment