Created
October 16, 2019 10:21
-
-
Save marcuszierke/a002e17e51ea0d5d9c94fe73cc6b39b6 to your computer and use it in GitHub Desktop.
React Contact Form without using state
This file contains 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, { useState } from 'react'; | |
const ContactForm = () => { | |
const [newsletterValue, setNewsletterValue] = useState(false); | |
function changeNewsletterValue() { | |
setNewsletterValue(!newsletterValue); | |
} | |
function handleSubmit(event) { | |
event.preventDefault(); | |
const data = {}; | |
const formElements = Array.from(event.target); | |
formElements.map(input => (data[input.name] = input.value)); | |
} | |
return ( | |
<div> | |
<form onSubmit={handleSubmit}> | |
<input name="firstName" type="text">First Name<input> | |
<input name="lastName" type="text">Last Name<input> | |
<input name="address" type="text">Adress<input> | |
<input name="zip" type="text">Zipcode<input> | |
<input name="city" type="text">City<input> | |
... | |
<select name="country"> | |
<option value="France">France<option> | |
<option value="USA">USA<option> | |
<option value="India">India<option> | |
<option value="China">China<option> | |
</select> | |
<input | |
type="checkbox" | |
name="newsletter" | |
onClick={changeNewsletterValue} | |
value={newsletterValue} | |
/> | |
<button type="submit">Submit</button> | |
</form> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment