Now before we move on to creating our sign up page, we are going to take a short detour to simplify how we handle form fields in React. We built a form as a part of our login page and we are going to do the same for our sign up page. You’ll recall that in our login component we were creating two state variables to store the username and password.

const [email, setEmail] = useState("");
const [password, setPassword] = useState("");

And we also use something like this to set the state:

onChange={e => setEmail(}

Now we are going to do something similar for our sign up page and it’ll have a few more fields than the login page. So it makes sense to simplify this process and have some common logic that all our form related components can share. Plus this is a good way to introduce the biggest benefit of React Hooks — reusing stateful logic between components.

Creating a Custom React Hook

Let’s pull the above logic into a custom React Hook.

Create a src/libs/ directory to store all our common code.

$ mkdir src/libs/

Add the following to src/libs/hooksLib.js.

import { useState } from "react";

export function useFormFields(initialState) {
  const [fields, setValues] = useState(initialState);

  return [
    function(event) {

Creating a custom hook is amazingly simple. Let’s go over how this works:

  1. A custom React Hook starts with the word use in its name. So ours is called useFormFields.

  2. Our Hook takes the initial state of our form fields as an object and saves it as a state variable called fields.

  3. It returns an array with fields and a function that sets the new state based on the event object. The only difference here is that we are using (which contains the id of our form field) to store the value (

And that’s it! We can now use this in our Login component.

Using Our Custom Hook

Replace our src/containers/Login.js with the following:

import React, { useState } from "react";
import { Auth } from "aws-amplify";
import { FormGroup, FormControl, ControlLabel } from "react-bootstrap";
import LoaderButton from "../components/LoaderButton";
import { useFormFields } from "../libs/hooksLib";
import "./Login.css";

export default function Login(props) {
  const [isLoading, setIsLoading] = useState(false);
  const [fields, handleFieldChange] = useFormFields({
    email: "",
    password: ""

  function validateForm() {
    return > 0 && fields.password.length > 0;

  async function handleSubmit(event) {


    try {
      await Auth.signIn(, fields.password);
    } catch (e) {

  return (
    <div className="Login">
      <form onSubmit={handleSubmit}>
        <FormGroup controlId="email" bsSize="large">
        <FormGroup controlId="password" bsSize="large">

You’ll notice that we are using our useFormFields Hook. A good way to think about custom React Hooks is to simply replace the line where we use it, with the Hook code itself. So instead of this line:

const [fields, handleFieldChange] = useFormFields({
  email: "",
  password: ""

Simply imagine the code for the useFormFields function instead!

Finally, we are setting our fields using the function our custom Hook is returning.


Now we are ready to tackle our sign up page.