Last active
February 15, 2020 15:56
-
-
Save jrwebdev/c356bd0f91b047ad67383ef97e3f29df to your computer and use it in GitHub Desktop.
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
export interface MyInputHandles { | |
focus(): void; | |
} | |
const MyInput: RefForwardingComponent<MyInputHandles, MyInputProps> = ( | |
props, | |
ref | |
) => { | |
const inputRef = useRef<HTMLInputElement>(null); | |
useImperativeHandle(ref, () => ({ | |
focus: () => { | |
if (inputRef.current) { | |
inputRef.current.focus(); | |
} | |
}, | |
})); | |
return <input {...props} ref={inputRef} />; | |
}; | |
export default forwardRef(MyInput); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment