Skip to content

Instantly share code, notes, and snippets.

@marcuszierke
Created October 16, 2019 10:21
Show Gist options
  • Save marcuszierke/a002e17e51ea0d5d9c94fe73cc6b39b6 to your computer and use it in GitHub Desktop.
Save marcuszierke/a002e17e51ea0d5d9c94fe73cc6b39b6 to your computer and use it in GitHub Desktop.
React Contact Form without using state
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