Last active
July 7, 2021 18:36
-
-
Save Shelob9/e53a23f06f3f8ef89f49886640262c20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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