Last active
April 18, 2020 14:01
-
-
Save rolspace/316f4269b36a885d5b8721b26ad44d52 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 { useState } from "react"; | |
export const useInput = initialValue => { | |
const [value, setValue] = useState(initialValue); | |
return { | |
value, | |
setValue, | |
reset: () => setValue(""), | |
bind: { | |
value, | |
onChange: event => { | |
setValue(event.target.value); | |
} | |
} | |
}; | |
}; |
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
export function NameForm(props) { | |
const { value:firstName, bind:bindFirstName, reset:resetFirstName } = useInput(''); | |
const { value:lastName, bind:bindLastName, reset:resetLastName } = useInput(''); | |
const handleSubmit = (evt) => { | |
evt.preventDefault(); | |
alert(`Submitting Name ${firstName} ${lastName}`); | |
resetFirstName(); | |
resetLastName(); | |
} | |
return ( | |
<form onSubmit={handleSubmit}> | |
<label> | |
First Name: | |
<input type="text" {...bindFirstName} /> | |
</label> | |
<label> | |
Last Name: | |
<input type="text" {...bindLastName} /> | |
</label> | |
<input type="submit" value="Submit" /> | |
</form> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment