import { useState } from "react";
const useInput = (initialValue, validator) => {
const [value, setValue] = useState(initialValue);
let checkValue = true;
const onChange = (e) => {
if (typeof validator === "function") {
checkValue = validator(value);
}
if (checkValue) setValue(e.target.value);
};
return { value, onChange };
};
const HookInputs = () => {
const validator = (value) => value.length < 8;
const user = useInput("", validator);
return (
<>
<input type="text" {...user} /> <button>입력</button>
</>
);
};
export default HookInputs;