Last active
February 9, 2023 22:36
-
-
Save Venryx/7cff24b17867da305fff12c6f8ef6f96 to your computer and use it in GitHub Desktop.
Using "useImperativeHandle" in a React functional component, with automatic TypeScript typing
This file contains hidden or 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 {forwardRef, useImperativeHandle, ForwardRefExoticComponent, RefAttributes, Ref} from "react"; | |
export type Handle<T> = T extends ForwardRefExoticComponent<RefAttributes<infer T2>> ? T2 : never; | |
export const Parent = (props: {})=> { | |
let childHandle: Handle<typeof Child>; | |
return ( | |
<div onClick={()=>childHandle.SayHi()}> | |
<Child name="Bob" ref={c=>childHandle = c}/> | |
</div> | |
); | |
}; | |
export const Child = forwardRef((props: {name: string}, ref: Ref<{SayHi}>)=> { | |
const {name} = props; | |
// expose internal functions; what we return in the callback below is what gets sent to the "ref" callback in Parent | |
useImperativeHandle(ref, () => ({ SayHi })); | |
function SayHi() { console.log("Saying hello from: " + name); } | |
return <div>{name}</div>; | |
}); |
Isn't React.ElementRef<typeof Child>
doing the same as type Handle<T> = T extends ForwardRefExoticComponent<RefAttributes<infer T2>> ? T2 : never;
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Venryx Yes you are right, when I changed 'strict' from true to false, the error disappeared, thanks!