Last active
August 14, 2022 01:55
-
-
Save eblind39/49ef244d5b2f13b05bcf8b8bd81296cf to your computer and use it in GitHub Desktop.
ReactJS & Typescript - Get elements from form in handle submit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {SyntheticEvent} from 'react' | |
const Login = () => { | |
const handleSubmit = async (evt: SyntheticEvent) => { | |
evt.preventDefault() | |
const target = evt.target as HTMLFormElement | |
const elements = target.elements as typeof target.elements & { | |
email: {value: string} | |
password: {value: string} | |
} | |
if (!email.value) setEmailValMsg('The email is required') | |
else setEmailValMsg('') | |
if (!password.value) setPasswordValMsg('The password is required') | |
else setPasswordValMsg('') | |
setIsFetching(true) | |
const strBody: BodyInit = `{"email":"${email}","password":"${password}"}` | |
await fetch('/login', { | |
method: 'POST', | |
mode: 'cors', | |
headers: { | |
Accept: 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
body: strBody, | |
}) | |
setIsFetching(false) | |
} | |
} | |
export default Login |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment