Skip to content

Instantly share code, notes, and snippets.

@rolspace
Last active April 18, 2020 14:01
Show Gist options
  • Save rolspace/316f4269b36a885d5b8721b26ad44d52 to your computer and use it in GitHub Desktop.
Save rolspace/316f4269b36a885d5b8721b26ad44d52 to your computer and use it in GitHub Desktop.
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);
}
}
};
};
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>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment