Created
December 2, 2019 13:32
-
-
Save rolandcoops/5a728c08331bd280a339f6f5055e41cd to your computer and use it in GitHub Desktop.
Basic form event delegation example
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, { useCallback, useState } from 'react' | |
const Form = () => { | |
const [profile, setProfile] = useState({ | |
firstName: 'John', | |
lastName: 'Johnsonian', | |
jobTitle: 'Jabberwocky', | |
}) | |
console.log(profile) | |
const handleChange = useCallback((event) => { | |
const { id, value } = event.target | |
setProfile({ ...profile, [id]: value }) | |
}, [profile, setProfile]) | |
return ( | |
<form onChange={handleChange} className="form"> | |
<div className="form-section"> | |
<label htmlFor="firstName">First Name: </label> | |
<input id="firstName" type="text" value={profile.firstName} /> | |
</div> | |
<div className="form-section"> | |
<label htmlFor="lastName">Last Name: </label> | |
<input id="lastName" type="text" value={profile.lastName} /> | |
</div> | |
<div className="form-section"> | |
<label htmlFor="jobTitle">Job Title: </label> | |
<input id="jobTitle" type="text" value={profile.jobTitle} /> | |
</div> | |
</form> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment