Skip to content

Instantly share code, notes, and snippets.

@LucasCalazans
Last active May 28, 2019 19:02
Show Gist options
  • Save LucasCalazans/cb653a56a48f8e53c539807b7eca8c9b to your computer and use it in GitHub Desktop.
Save LucasCalazans/cb653a56a48f8e53c539807b7eca8c9b to your computer and use it in GitHub Desktop.
Form validation without useMemo
const rules = [
'8 characteres',
'lowercase word',
'uppercase word',
];
// Lista de erros
const Errors = ({ active }) => {
return active && (
<ul>
{rules.map((value, index) => (
<li key={index}>{value}</li>
))}
</ul>
);
}
const CreateAccountForm = () => {
const [password, setPassword] = useState('');
const [showErrors, setShowErrors] = useState(false);
const checkPassword = event => {
const { value } = event.target;
setShowErrors(value.length < 8);
setPassword(value);
};
return (
<form>
<h1>Login</h1>
<input placeholder="Email" />
<input
type="password"
placeholder="Password"
value={password}
onChange={checkPassword}
/>
<Errors active={showErrors} />
</form>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment