Created
December 16, 2018 01:15
-
-
Save jeanbauer/00c06ddfa44a199dd9aa018c1f4775f2 to your computer and use it in GitHub Desktop.
Text Input using react hooks with a custom hook: useFormInput
This file contains hidden or 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' | |
const Form = () => { | |
const name = useFormInput('Name') | |
return ( | |
<> | |
<input {...name} /> | |
</> | |
) | |
} | |
function useFormInput(initialValue) { | |
const [value, setValue] = useState(initialValue) | |
function handleChange(e) { | |
setValue(e.target.value) | |
} | |
return { | |
value, | |
onChange: handleChange | |
} | |
} | |
export default Form |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment