Skip to content

Instantly share code, notes, and snippets.

@rolandcoops
Created December 2, 2019 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rolandcoops/5a728c08331bd280a339f6f5055e41cd to your computer and use it in GitHub Desktop.
Save rolandcoops/5a728c08331bd280a339f6f5055e41cd to your computer and use it in GitHub Desktop.
Basic form event delegation example
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