Skip to content

Instantly share code, notes, and snippets.

@shameemreza
Created April 2, 2023 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shameemreza/74b371fbb9d716fba7817684186efc39 to your computer and use it in GitHub Desktop.
Save shameemreza/74b371fbb9d716fba7817684186efc39 to your computer and use it in GitHub Desktop.
Code for "Exposing React Component functions with fowardRef and useRef" Article
import { forwardRef, useRef } from 'react';
const Input = forwardRef((props, ref) => {
const inputRef = useRef(null);
const getValue = () => {
return inputRef.current.value;
}
const isValid = () => {
const value = getValue();
return value !== '' && value.length >= 3;
}
return (
<input
type="text"
ref={inputRef}
{...props}
/>
)
});
export { Input };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment