Skip to content

Instantly share code, notes, and snippets.

@LucasCalazans
Last active March 3, 2022 22:45
Show Gist options
  • Save LucasCalazans/f66a6789648bd5d2e8e1a97468ba1a28 to your computer and use it in GitHub Desktop.
Save LucasCalazans/f66a6789648bd5d2e8e1a97468ba1a28 to your computer and use it in GitHub Desktop.
Form validation using useMemo
const CreateAccountForm = () => {
const [password, setPassword] = useState('');
const [showErrors, setShowErrors] = useState(false);
const checkPassword = event => {
const { value } = event.target;
setShowErrors(value.length < 8);
setPassword(value);
}
const memoizedErrors = useMemo(() => <Errors active={showErrors} />, [showErrors]);
return (
<form>
<h1>Login</h1>
<input placeholder="Email" />
<input type="password" placeholder="Password" value={password} onChange={checkPassword} />
{memoizedErrors}
</form>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment