Skip to content

Instantly share code, notes, and snippets.

@maurocen
Created July 5, 2020 20:41
Show Gist options
  • Save maurocen/bd68a31df34783dcdbc62ce8613cd4b1 to your computer and use it in GitHub Desktop.
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.
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