Created
October 5, 2020 07:05
-
-
Save nofanto/d29f845a696ee3a91d8f4e3eaec239be to your computer and use it in GitHub Desktop.
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 from 'react'; | |
function Form(props) { | |
const data = { | |
name: '', | |
job: '' | |
}; | |
const nameRef = React.createRef(); | |
const jobRef = React.createRef(); | |
const handleChange = event => { | |
const { name, value } = event.target; | |
data[name] = value; | |
} | |
const onFormSubmit = (event) => { | |
event.preventDefault(); | |
props.handleSubmit({...data}); | |
nameRef.current.value = ''; | |
jobRef.current.value = ''; | |
} | |
return ( | |
<form onSubmit={onFormSubmit}> | |
<label htmlFor="name">Name</label> | |
<input | |
type="text" | |
name="name" | |
id="name" | |
ref={nameRef} | |
onChange={handleChange} /> | |
<label htmlFor="job">Job</label> | |
<input | |
type="text" | |
name="job" | |
id="job" | |
ref={jobRef} | |
onChange={handleChange} /> | |
<button type="submit"> | |
Submit | |
</button> | |
</form> | |
); | |
} | |
export default Form; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment