Skip to content

Instantly share code, notes, and snippets.

@nofanto
Created October 5, 2020 07:05
Show Gist options
  • Save nofanto/d29f845a696ee3a91d8f4e3eaec239be to your computer and use it in GitHub Desktop.
Save nofanto/d29f845a696ee3a91d8f4e3eaec239be to your computer and use it in GitHub Desktop.
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