Skip to content

Instantly share code, notes, and snippets.

@LeandrodeLimaC
Created March 21, 2024 16:40
Show Gist options
  • Save LeandrodeLimaC/498552191829f10256715d80b3e39afb to your computer and use it in GitHub Desktop.
Save LeandrodeLimaC/498552191829f10256715d80b3e39afb to your computer and use it in GitHub Desktop.
This updated version of ForwardRef was created to be used with generics typescript can't infer correctly the type of the reference in this scenario. The downside is that using this function, we will lose the dsiplayName property and some other stuff like defaultProperties. ref: https://fettblog.eu/typescript-react-generic-forward-refs/#option-3%…
/**
* This updated version of ForwardRef was created to be used with generics
* typescript can't infer correctly the type of the reference in this scenario.
*
* The downside is that using this function, we will lose the dsiplayName property
* and some other stuff like defaultProperties.
*
* ref: https://fettblog.eu/typescript-react-generic-forward-refs/#option-3%3A-augment-forwardref
*/
export type FixedForwardRef = <T, P = {}>(
render: (props: P, ref: React.Ref<T>) => JSX.Element,
) => (props: P & React.RefAttributes<T>) => JSX.Element
export const forwardRef = reactForwardRef as FixedForwardRef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment