bind input value to useState code example
Example 1: bind input value to useState
export function NameForm(props) {
const { value:firstName, bind:bindFirstName, reset:resetFirstName } = useInput('');
const { value:lastName, bind:bindLastName, reset:resetLastName } = useInput('');
const handleSubmit = (evt) => {
evt.preventDefault();
alert(`Submitting Name ${firstName} ${lastName}`);
resetFirstName();
resetLastName();
}
return (
<form onSubmit={handleSubmit}>
<label>
First Name:
<input type="text" {...bindFirstName} />
</label>
<label>
Last Name:
<input type="text" {...bindLastName} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
Example 2: bind input value to useState
import { useState } from "react";
export const useInput = initialValue => {
const [value, setValue] = useState(initialValue);
return {
value,
setValue,
reset: () => setValue(""),
bind: {
value,
onChange: event => {
setValue(event.target.value);
}
}
};
};