import React, { useState } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const useInput = (initValue, validator) => {
const [value, setValue] = useState(initValue);
const onChange = (event) => {
const {
target: { value }
} = event;
let willUpdate = true;
if (typeof validator === "function") {
willUpdate = validator(value);
}
if (willUpdate) {
setValue(value);
}
};
return { value, onChange };
};
const App = () => {
const maxLength = (value) => value.length <=10;
const name = useInput("Mr.", maxLength);
return (
<div className="App">
{}
<input placeholder="What's your name?" {...name} />
</div>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);