Skip to content

Instantly share code, notes, and snippets.

@eblind39
Last active August 14, 2022 01:55
Show Gist options
  • Save eblind39/49ef244d5b2f13b05bcf8b8bd81296cf to your computer and use it in GitHub Desktop.
Save eblind39/49ef244d5b2f13b05bcf8b8bd81296cf to your computer and use it in GitHub Desktop.
ReactJS & Typescript - Get elements from form in handle submit
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