Skip to content

Instantly share code, notes, and snippets.

@jrwebdev
Last active February 15, 2020 15:56
Show Gist options
  • Save jrwebdev/c356bd0f91b047ad67383ef97e3f29df to your computer and use it in GitHub Desktop.
Save jrwebdev/c356bd0f91b047ad67383ef97e3f29df to your computer and use it in GitHub Desktop.
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