From da1854e8fd34245e2e9e4fd941320f08b1cc40e1 Mon Sep 17 00:00:00 2001 From: ilyayudovin Date: Fri, 28 Aug 2020 18:55:34 +0300 Subject: add useMemo for check validity of the form --- src/containers/Registration/Registration.tsx | 41 ++++++++++++++-------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/containers/Registration/Registration.tsx b/src/containers/Registration/Registration.tsx index 47b5947..3269f6e 100644 --- a/src/containers/Registration/Registration.tsx +++ b/src/containers/Registration/Registration.tsx @@ -1,4 +1,4 @@ -import React, {useState, useRef, FormEvent} from 'react'; +import React, {useState, useRef, FormEvent, useMemo} from 'react'; import { useHistory } from 'react-router-dom'; import { makeStyles } from '@material-ui/core/styles'; import { @@ -39,16 +39,16 @@ const useStyles = makeStyles(theme => ({ })); interface ErrorStates { - validUsername: boolean | undefined; - validEmail: boolean | undefined; - validPassword: boolean | undefined; + username: boolean | undefined; + email: boolean | undefined; + password: boolean | undefined; } const Registration: React.FC = () => { const [values, setValues] = useState({ - validUsername: undefined, - validEmail: undefined, - validPassword: undefined, + username: undefined, + email: undefined, + password: undefined, }); const [showPassword, setShowPassword] = useState(false); @@ -59,16 +59,16 @@ const Registration: React.FC = () => { const { login } = useAuth(); const history = useHistory(); - const checkFromValidation = () => { - return values.validUsername && values.validEmail && values.validPassword; - }; + const isValid = useMemo(() => { + return values.username && values.email && values.password; + },[values]); const handleSubmit = (event: FormEvent) => { event.preventDefault(); const username = usernameRef.current?.value?.toLowerCase(); const password = passwordRef.current?.value; const email = emailRef.current?.value; - if (username && password && checkFromValidation()) { + if (username && password && isValid) { console.log('yes'); post('/users', { username, password, email }) .then(() => login(username, password)) @@ -87,13 +87,13 @@ const Registration: React.FC = () => { event.preventDefault(); }; const handleUsernameChange = (e: React.ChangeEvent) => { - setValues({ ...values, validUsername: e.currentTarget.value.length > 0 }); + setValues({ ...values, username: e.currentTarget.value.length > 0 }); }; const handleEmailChange = (e: React.ChangeEvent) => { - setValues({ ...values, validEmail: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(e.currentTarget.value) }); + setValues({ ...values, email: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(e.currentTarget.value) }); }; const handlePasswordChange = (e: React.ChangeEvent) => { - setValues({ ...values, validPassword: e.currentTarget.value.length > 6 }); + setValues({ ...values, password: e.currentTarget.value.length > 6 }); }; return ( @@ -103,24 +103,24 @@ const Registration: React.FC = () => { { > {showPassword ? : } - {values.validPassword && values.validPassword !== undefined && } ) }} -- cgit v1.2.3