Created
July 5, 2020 20:41
-
-
Save maurocen/bd68a31df34783dcdbc62ce8613cd4b1 to your computer and use it in GitHub Desktop.
A hook to handle input value and if said value has changed. Allows resetting value to initial or new 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
import { useState } from 'react'; | |
const useInput = (initialValue = '') => { | |
const [value, setValue] = useState(initialValue); | |
const [touched, setTouched] = useState(false); | |
const onChange = (newValue) => { | |
setValue(newValue); | |
setTouched(true); | |
} | |
const reset = (resetValue = initialValue) => { | |
setValue(resetValue); | |
setTouched(false); | |
} | |
return [value, onChange, touched, reset]; | |
} | |
export default useInput; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment