Skip to content

Instantly share code, notes, and snippets.

@mohanramphp
Last active January 9, 2019 08:00
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 mohanramphp/e2ec0f7044f5c6a85a3e4c292ee1d64e to your computer and use it in GitHub Desktop.
Save mohanramphp/e2ec0f7044f5c6a85a3e4c292ee1d64e to your computer and use it in GitHub Desktop.
Login component using React hooks
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