Skip to content

Instantly share code, notes, and snippets.

@jeanbauer
Created December 16, 2018 01:15
Show Gist options
  • Save jeanbauer/00c06ddfa44a199dd9aa018c1f4775f2 to your computer and use it in GitHub Desktop.
Save jeanbauer/00c06ddfa44a199dd9aa018c1f4775f2 to your computer and use it in GitHub Desktop.
Text Input using react hooks with a custom hook: useFormInput
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