Skip to content

Instantly share code, notes, and snippets.

@zeuslawyer
Created September 22, 2019 06:15
Show Gist options
  • Save zeuslawyer/d1a1e11dcc1ca85c3f87e20403eac675 to your computer and use it in GitHub Desktop.
Save zeuslawyer/d1a1e11dcc1ca85c3f87e20403eac675 to your computer and use it in GitHub Desktop.
react hooks based controlled form
import React , {useState} from "react";
import ReactDOM from "react-dom";
import "./styles.css";
function App() {
const defaultFormState = {
first: '',
second: '',
third: '',
fourth: ''
}
const [inputs, setInputs] = useState(defaultFormState)
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<input name="first" value ={inputs.first} onChange= {(e)=>setInputs({...inputs, [e.target.name]: e.target.value})} />
<input name="second" value = {inputs.second} onChange= {(e)=>setInputs({...inputs, [e.target.name]: e.target.value})} />
<input name="third" value = {inputs.third} onChange= {(e)=>setInputs({...inputs, [e.target.name]: e.target.value})} />
<input name="fourth" value = {inputs.fourth} onChange= {(e)=>setInputs({...inputs, [e.target.name]: e.target.value})} />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment