Skip to content

Instantly share code, notes, and snippets.

@davemackintosh
Last active October 11, 2019 10:48
Show Gist options
  • Save davemackintosh/68cca9b35b37b674790f8e0f76af206a to your computer and use it in GitHub Desktop.
Save davemackintosh/68cca9b35b37b674790f8e0f76af206a to your computer and use it in GitHub Desktop.
import React from "react"
export const MyFormWithState = (props) => {
const [formState, setFormState] = useState({
name: "",
age: 0,
})
const onChange = (event) =>
setFormState({
...formState,
[event.currentTarget.name]: event.currentTarget.value,
})
return (
<form onSubmit={props.onSubmit}>
<fieldset>
<label htmlFor="name">Name</label>
<input
onChange={onChange}
name="name"
type="text"
value={formState.name}
/>
<label htmlFor="age">Age</label>
<input
onChange={onChange}
name="age"
type="number"
value={formState.age}
/>
</fieldset>
</form>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment