import { useState } from 'react';
function useInput(validateValue) {
const [enteredValue, setEnteredValue] = useState('');
const [isTouched, setIsTouched] = useState(false);
//이거는 props로 받기
// const valueIsValid = enteredValue.trim() !== '';
const valueIsValid = validateValue(enteredValue);
const hasError = !valueIsValid && isTouched;
const valueInputChangeHandler = (event) => {
setEnteredValue(event.target.value);
};
const valueInputBlurHandler = (event) => {
setIsTouched(true);
};
const reset = () => {
setEnteredValue('');
setIsTouched(false);
};
return {
value: enteredValue,
isValid: valueIsValid,
hasError,
valueInputBlurHandler,
valueInputChangeHandler,
reset,
};
}
export default useInput;