React Projects

Create an Address Book with React and JavaScript

Spread the love

React is an easy to use JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create an address book app with React and JavaScript.

Create the Project

We can create the React project with Create React App.

To install it, we run:

npx create-react-app address-book

with NPM to create our React project.

Also, we’ve to install the uuid package to let us assign unique IDs to the contact entries.

To do this, we run:

npm i uuid

Create the Address Book App

To create the address book app, we write:

import React, { useState } from "react";
import { v4 as uuidv4 } from "uuid";

export default function App() {
  const [contact, setContact] = useState({
    name: "",
    address: "",
    phone: ""
  const [contacts, setContacts] = useState([]);

  const addContact = (e) => {
    const { name, address, phone } = contact;
    const formValid =
      name &&
      address &&
    if (!formValid) {
    setContacts((contacts) => [
      { id: uuidv4(), name, address, phone }

  const deleteContact = (index) => {
    setContacts((contacts) => contacts.filter((_, i) => i !== index));

  return (
    <div className="App">
      <form onSubmit={addContact}>
            onChange={(e) =>
              setContact((contact) => ({, name: }))
            onChange={(e) =>
              setContact((contact) => ({, address: }))
            onChange={(e) =>
              setContact((contact) => ({, phone: }))
        <button type="submit">add</button>
      {, index) => {
        return (
          <div key={}>
            <p>name : {}</p>
            <p>address : {contact.address}</p>
            <p>phone : {}</p>
            <button type="button" onClick={() => deleteContact(index)}>

We define the contact state that’s used to store the form’s data.

And the contacts state is defined to store the contacts entries.

Next, we define the addContact function.

Inside it, we call e.preventDefault() to do client-side submission.

Then we check if name , address and phone values are valid.

If they are, then we call setContacts with a callback that returns a copy of the contacts array with a new entry at the end to add the entry.

uuidv4 returns the unique ID for the contact.

The deleteContact removes an entry by calling setContacts with a callback that returns a copy of the contacts array without the item at the given index .

Below that, we add a form with the onSubmit prop set to addContact so addContact can be run when we click on the button with type submit .

Inside the form, we define 3 inputs with the value prop to get the value from the state.

And their onChange props are set to functions that call setContact with a callback that returns a copy of the contact value but with the new value for the property that it sets. has the inputted value.

Below the form, we render the contacts state by calling map with a callback that returns a div with the property values of each contact entry displayed.

The key prop is set to so that React can distinguish between the entries.

And below that, we show the delete that calls deleteContact with index when we click it.


We can create an address book app easily with React and JavaScript.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.