Created
September 9, 2022 00:04
-
-
Save HughParsons/f90142528e498d50e84876a9f550a5ad to your computer and use it in GitHub Desktop.
React HOC (higher order component) with forwardRef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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