Last active
January 9, 2019 08:00
-
-
Save mohanramphp/e2ec0f7044f5c6a85a3e4c292ee1d64e to your computer and use it in GitHub Desktop.
Login component using React hooks
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
const Login = () => { | |
// handling complex state with useState | |
const [loginDetails, setLoginDetails] = useState({username: '', password: ''}) | |
handleLogin = () => { | |
... | |
} | |
return ( | |
<div> | |
<form> | |
{/** setLoginDetail method is called by passing a fat function which has prev state as argument */} | |
<input id='username' onInput={() => setLoginDetails(prev => ({ ...prev, username: this.value }))} /> | |
<input id='password' onInput={() => setLoginDetails(prev => ({ ...prev, password: this.value }))} /> | |
<button onClick={()=>handleLogin(loginDetails)}>Login</button> | |
</form> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment