Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active July 7, 2021 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/e53a23f06f3f8ef89f49886640262c20 to your computer and use it in GitHub Desktop.
Save Shelob9/e53a23f06f3f8ef89f49886640262c20 to your computer and use it in GitHub Desktop.
import { useRef } from "react";
export default function Form({ onSubmit }) {
const inputEl = useRef(null);
const handleSubmit = (e) => {
e.preventDefault();
onSubmit({
input: inputEl.current.value,
});
}
return (
<form onSubmit={handleSubmit}>
<label for="input">Input</label>
<input ref={inputEl} id="input" />
<button type="submit" onClick={handleSubmit} />
</form>
);
}
import { useState } from "react";
export default function Form({ onSubmit }) {
const [state,setState]= useState({input: ''});
const handleSubmit = (e) => {
e.preventDefault();
onSubmit(state);
}
return (
<form onSubmit={handleSubmit}>
<label for="input">Input</label>
<input value={state.input} onChange={(e)=> setState({...state,input:e.target.value})} id="input" />
<button type="submit" onClick={handleSubmit} />
</form>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment