To-do applications are one of the ways you can use to manage a set of tasks. As developers, learning how to build a to-do application will also help to understand certain concepts one of which includes an understanding of how to build an application with a database.

In this article, you will learn how to build a to-do web app by making use of React.js and Firebase Database.

Table of Contents

  • prerequisites
  • How to Create the Firebase Project
  • Creating the Firestore Database
  • How to Create the React Project
  • Setting up the Project Structure
  • How to Integrate Firebase in React
  • Integrate Bootstrap 5 into React
  • Designing the User Interface
  • Adding Data to the Firestore in React
  • Fetching Data from the Firestore in React
  • Deleting Data from the Firestore in React
  • Updating Data in the Firestore in React
  • How to Integrate the Checkbox Functionality
  • How to order Data by Timestamp in Firebase
  • Conclusion

Watch the video version of this article below:

[embedded content]

Prerequisites

To install the npm packages needed for this React application such as Firebase, you need to have Node.js downloaded.

Visual Studio code serves as the code editor we will use to build this project.

The Firebase Console serves as the backend as a service database that helps us to store and manage our data, through the use of the Cloud Firestore.

How to Setup the Firebase Project

To set up Firebase, you head to the Firebase console website and create a new project by using your Google account. Once you are logged into Firebase you will see an existing project, you can click on the add project button, as seen below.

Firebase dashboard

Firebase dashboard

After clicking on the add project button, we get navigated to a new page which requires 3 steps before the Firebase project is created:

  • The first step requires us to name the Firebase project, which we will call todo.
  • The second step asks if we want to enable Google Analytics, you should disable it by using the toggle button.
  • Finally, we can now click on the create project button.

Once the project is created, we click on the continue button which navigates us to the next screen, which is now our default Firebase project dashboard.

Firebase dashboard

Firebase dashboard

We have now completed the creation of a new Firebase project.

Creating the Firestore Database

Inside the Firebase dashboard, on the left-hand panel, we take the following steps:

  • Click on the Build dropdown.
  • Within the Build dropdown, select Firestore Database, this displays a page where we can click on the Create database button.
  • Next, a modal pops up asking if we want Production mode or Test mode. You can choose Test mode since the app is currently in the development stage.
  • The next step asks for where we want our Cloud Firestore to be located, you can choose the location closest to your area due to the possibility of latency.

Once we click on enable, we get redirected to the Cloud Firestore page which will currently have an empty Collection.

How to Create the React Project

We are going to create a new React project by making use of CRA (Create-React-App). Since we have node.js installed, we can run the following commands:

JavaScript

 

npm i create-react-app

Once the installation of CRA is complete, we can now create a new React project with the following command below:

JavaScript

 

npm init react-app todo cd todo code .

We have now created a new React project called todo, navigated into the project directory, and opened the project in Visual Studio Code.

We can now begin the setup of the React application.

Setting up the Project Structure

To set up the architecture of our React project, you can implement the steps below:

  • In the src directory, create two folders named components and services.
  • The components folder will contain two new document files. The first js file is called Todo.js, while the second file is called EditTodo.js.
  • In the services folder, create a js file called firebase.config.js. This file will contain the configuration of the firebase which we can export to different components.
  • Finally, still within the src directory, we head to the App.js file. Here, we clear the boilerplate inside of  div with the className of App and then import the Todo.js component as seen in the following lines of code:
JavaScript

 

<div class="codeMirror-code–wrapper" data-code="import './App.css'; import Todo from './components/Todo'; function App() { return (

 

 ); } export default App;” data-lang=”text/javascript”>

import './App.css'; import Todo from './components/Todo'; function App() { return ( <div className="App">  <Todo ></Todo> </div>  ); } export default App;

With the above, we now have the basic structure of our React project set up.

How to Integrate Firebase in React

To add the Firebase web SDK to our new app, the first thing we need to do is run the following command inside our terminal:

JavaScript

 

npm install firebase

Next, you open up the firebase.config.js file inside of the services folder and then use the following imports to configure firebase into the React app:

JavaScript

 

import { initializeApp } from "firebase/app"; import { getFirestore } from "firebase/firestore";

Furthermore, you need to grab the project configuration settings from the Firebase dashboard. On the left-hand panel of the dashboard, click on project overview and select project settings.

Scroll to the bottom of the page and select the web icon as shown below:

Web icon for Firebase project settings

Web icon for Firebase project settings

Once the web icon gets selected, a new page shows up asking you to give the app a nickname. You can proceed to call it todo or any other word you prefer, then click on register app. Firebase will now generate the Firebase configuration settings, which contains the spiky, storage bucket, auth domain, project id, app id, etc. as seen below:

Firebase config settings

Firebase config settings

We can now grab this and paste it inside our firebase.config.js file:

JavaScript

 

import { initializeApp } from "firebase/app"; import { getFirestore } from "firebase/firestore"; // Your web app's Firebase configuration const firebaseConfig = {     apiKey: "AIzaSyC5u80wO6iaPl8E9auM0IRXliYGKyDQHfU",     authDomain: "todo-b74fc.firebaseapp.com",     projectId: "todo-b74fc",     storageBucket: "todo-b74fc.appspot.com",     messagingSenderId: "872116099545",     appId: "1:872116099545:web:9bb66d12ca15f2f39521c8" };

The final step needed to complete the Firebase configuration is to initialize Firebase by making use of the config variable and then export it so it becomes available in all our components, as seen in the following lines of code:

JavaScript

 

import { initializeApp } from "firebase/app"; import { getFirestore } from "firebase/firestore"; // Your web app's Firebase configuration const config = {     apiKey: "AIzaSyC5u80wO6iaPl8E9auM0IRXliYGKyDQHfU",     authDomain: "todo-b74fc.firebaseapp.com",     projectId: "todo-b74fc",     storageBucket: "todo-b74fc.appspot.com",     messagingSenderId: "872116099545",     appId: "1:872116099545:web:9bb66d12ca15f2f39521c8" }; const app = initializeApp(config); export const db = getFirestore(app);

With this, our Firebase configuration is successfully created and we do not need to make use of any other Firebase services.

Integrate Bootstrap 5 into React

To integrate Bootstrap 5 you will need to head over to the Bootstrap 5 website and grab the CDN link for both the CSS and JavaScript. You can then head back to the React project in VS Code, open the public directory, and proceed to select the index.html file.

In the index.html file, we can paste the CDN links for both the CSS and JavaScript within the head section. With that, we should have the result below:

<div class="codeMirror-code–wrapper" data-code=" ” data-lang=”text/html”>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> 

Now we have access to all Bootstrap 5 classes across our components.

Designing the User Interface

To implement the design for the React project you will start by clearing the boilerplate code inside of the App.css file. You can now proceed to open the index.css file and then paste the following styles:

body{   margin-top:20px;   background: #f8f8f8; } .todo-list {   margin: 10px 0 } .todo-list .todo-item {   padding: 15px;   margin: 5px 0;   border-radius: 0; } div.checker {   width: 18px;   height: 18px } div.checker input{   width: 18px;   height: 18px } div.checker {   display: inline-block;   vertical-align: middle; } .done {   text-decoration: line-through; }

Above, what we did was ensure all the elements that will be displayed on the browser are properly arranged.

Next, proceed to the Todo.js file and paste the code below within the return statement:

JavaScript

 

{   return (      

     

       

         

           

                           

             

               


                                 

                                                                       

                    Go hard or Go Home
                      10/11/2022                                                                                                  

                 

                 

               

             

           

         

          {/* Modal */}        

         

                       

             

               

                 Add Todo                

                           

             

                             

             

                                                 

           

                 

     

      ) } export default Todo” data-lang=”text/javascript”>

import React from 'react' const Todo = () => {   return (   <>     <div className="container">       <div className="row">         <div className="col-md-12">           <div className="card card-white">             <div className="card-body">               <button                 data-bs-toggle="modal"                 data-bs-target="#addModal"                 type="button"                 className="btn btn-info">Add Todo               </button>              <div className="todo-list">               <div className="todo-item">                <hr />                 <span>                  <div className="checker" >                  <span className="" >                   <input                     type="checkbox"                    />                  </span>                   </div>                     Go hard or Go Home<br />                       <i>10/11/2022</i>                  </span>                   <span className=" float-end mx-3">                      <EditTodo ></EditTodo></span>                     <button                       type="button"                       className="btn btn-danger float-end"                     >Delete</button>                    </div>                   </div>                  </div>                 </div>               </div>             </div>           </div>           {/* Modal */}        <div          className="modal fade"          id="addModal"          tabIndex="-1"          aria-labelledby="addModalLabel"          aria-hidden="true">          <div className="modal-dialog">           <form className="d-flex">              <div className="modal-content">              <div className="modal-header">                <h5                  className="modal-title"                  id="addModalLabel">                  Add Todo                </h5>              <button                 type="button"                 className="btn-close"                 data-bs-dismiss="modal"         &nb
sp;       aria-label="Close">              </button>              </div>              <div className="modal-body">                 <input                   type="text"                   className="form-control"                   placeholder="Add a Todo"                 />              </div>               <div className="modal-footer">                  <button                    className="btn btn-secondary"                    data-bs-dismiss="modal">Close                  </button>                  <button className="btn btn-primary">Create                          Todo</button>               </div>             </div>           </form>         </div>       </div>     </>   ) } export default Todo

In the above code:

  • The div with the className of .container displays the card that contains all items in our Todo list, while the div with the id of addModal contains the modal where all the todo can be created using the save button called Add todo.

The final part of our design is in the EditTodo.js file. The EditTodo.js file will only contain the modal that allows us to edit each to-do list item. The code can be seen below:

JavaScript

 

{  return (                    

       

         

           

             

                Update Todo Details              

                           

             

                                                           

             

                                             

           

         

       

          )   } export default EditTodo” data-lang=”text/javascript”>

import React from 'react' const EditTodo = () => {  return (      <>       <button          type="button"          className="btn btn-primary"          data-bs-toggle="modal"          data-bs-target="#exampleModal"        >           Edit Todo        </button>        <div          className="modal fade"          id="exampleModal"          tabIndex="-1"          aria-labelledby="editLabel"          aria-hidden="true">        <div className="modal-dialog">           <div className="modal-content">             <div className="modal-header">               <h5                 className="modal-title"                 id="editLabel">                 Update Todo Details               </h5>               <button                   type="button"                   className="btn-close"                   data-bs-dismiss="modal"                   aria-label="Close">               </button>              </div>              <div className="modal-body">               <form className="d-flex">                  <input                    type="text"                    className="form-control"                  />               </form>               </div>               <div className="modal-footer">                <button                  type="button"                  className="btn btn-secondary"                  data-bs-dismiss="modal">Close                </button>                <button                  type="button"                  className="btn btn-primary"                >Update Todo</button>               </div>             </div>           </div>         </div>       </>     )   } export default EditTodo

The design for the application is now complete. If you run the command npm start in the terminal and the code compiles, you should see the result below in the browser:

Complete page design

Complete page design

Adding Data to the Firestore in React

To implement the Add data functionality in our todo application, we start by importing some modules from Firebase/firestorm which can be seen below:

JavaScript

 

import { collection, addDoc, serverTimestamp } from 'firebase/firestore'

  • A Collection is a folder that contains Documents and Data. All the data saved on our Todo application will be saved in a Collection called, which we will create soon.
  • addDoc is a high-level method used to add data to a Collection?
  • The serverTimestamp contains the values of both time and date values for each Document in a Collection.

We then need to import the firebase.config.js file in our Todo.js file to allow us to have access to the firebase methods:

JavaScript

 

import { db } from '../services/firebase.config'

Using the import from our firebase.config.js file, we can now instantiate a reference to our Collection:

JavaScript

 

const collectionRef = collection(db, 'todo');

  • As seen in the code above, we created a variable called collectionRef. The collectionRef the variable takes in the collection method. This method has two arguments. The first argument called db references the firebase service, while the second argument will create a new Collection called todo, which will contain all necessary Documents created.

Next, we create two variables using the useStatehook:

JavaScript

 

const [createTodo, setCreateTodo] = useState("")

  • The first thing we did here is to import the state hook in React:
JavaScript

 

import React, { useState } from 'react

  • Then we created a getter and a setter called createTodo and setCreateTodorespectively.

To proceed, we move to the modal created within the JSX and implement the next couple of things:

  • Within the form tag, we create an onSubmit event handler called submitTodo.
<div class="codeMirror-code–wrapper" data-code="” data-lang=”text/html”>

<form className="d-flex" onSubmit={submitTodo}>

  • In the input tag within the form tag, we create an `onChange` event handler that allows us to get the value typed inside of the form:
JavaScript

 

setCreateTodo(e.target.value)} ” data-lang=”text/javascript”>

onChange={(e) => setCreateTodo(e.target.value)}

The final implementation we need to make before adding data to the database becomes functional is to configure the onSubmitevent handler previously created. The code for this can be seen below:

JavaScript

 

{ e.preventDefault(); try {    await addDoc(collectionRef, {    todo: createTodo,    isChecked: false,    timestamp: serverTimestamp()   })    window.location.reload();   } catch (err) {     console.log(err);   } }” data-lang=”text/javascript”>

//Add Todo Handler const submitTodo = async (e) => { e.preventDefault(); try {    await addDoc(collectionRef, {    todo: createTodo,    isChecked: false,    timestamp: serverTimestamp()   })    window.location.reload();   } catch (err) {     console.log(err);   } }

  • Above, we made the submitTodovariable asynchronous by making use of the async/await keyword in JavaScript.
  • We then created a parameter in the arrow function called, which serves as an event. This ensures we are able to make use of the e.preventDefault() method which prevents the form from reloading after submission.
  • Next within the try/catch block, we call the addDocmethod which takes two arguments. The first argument is the collectionRefvariable we created previously, while the second argument contains the object to be passed into the Firestore database.

These objects include the todo values inside of the input field, the checkbox value which is currently set as false, and then the timestamp in which the todo was created in the database.

  • We then make use of the window.location.reload()function in JavaScriptto refresh the form upon successful submission, while making use of catch to handle the error.

With this, we can now create a new to-do and view it in our database.

Creating the Todo

Creating the Todo

Fetching Data from the Firestore in React

To fetch the data from the Firestore in Firebase, we need to make two importation in our Todo.js file. These include the useEffect hook in React and thegetDocs from firebase/firestore:

JavaScript

 

import React, { useState, useEffect } from 'react' import { collection, addDoc, serverTimestamp, getDocs } from 'firebase/firestore'

We then need to create the setter(setTodo) and getter(todos) variables to help us access the data from the Firestore:

JavaScript

 

const [todos, setTodo] = useState([])

The data can now be fetched inside of the useEffecthook:

JavaScript

 

{ const getTodo = async () => {    await getDocs(collectionRef).then((todo) => {    let todoData = todo.docs.map((doc) => ({ …doc.data(), id:     doc.id }))    setTodo(todoData) }).catch((err) => {    console.log(err); }) }    getTodo() }, [])” data-lang=”text/javascript”>

useEffect(() => { const getTodo = async () => {    await getDocs(collectionRef).then((todo) => {    let todoData = todo.docs.map((doc) => ({ ...doc.data(), id:     doc.id }))    setTodo(todoData) }).catch((err) => {    console.log(err); }) }    getTodo() }, [])

  • Inside the useEffecthook, we created a variable called getTodo that takes in an asynchronous arrow function.
  • Then we called the getDocsmethod from Firebase. The getDocs method requires an argument, so we pass in the collectionRef. The getDocs returns a promise that we need to chain to using .then.
  • The promise returns an array that we need to map through to access the required data from the database, which are the todo list items as well as the id.
  • The todoDatavariable holds the data coming from the database. To have access to the data in our JSX, we will then put the todoDataas an argument in our setter which is called setTodo.
  • We then proceed to handle the error in case there is any using the catch keyword before we finally call the getTodofunction to initialize it on page load.

Now that we have access to our data from the database, we need to make it visible on the page. The data we have comes in the form of an array and we need to loop through it in the HTML file. This will be done within the divwith the classNameof  todo-list as seen below:

JavaScript

 

 

   

     


             

                                       

            {todo}
           10/11/2022                                                            

 

)}” data-lang=”text/javascript”>

{todos.map(({ todo, id }) =>  <div className="todo-list" key={id}>    <div className="todo-item">      <hr />       <span>        <div className="checker" >          <span className="" >           <input             type="checkbox"           />           </span>           </div>             {todo}<br />            <i>10/11/2022</i>           </span>           <span className=" float-end mx-3">            <EditTodo ></EditTodo>           </span>          <button             type="button"             className="btn btn-danger float-end"          >Delete          </button>        </div>   </div> )}

  • In the code above, we call the todosgetter that contains our data which comes in an array format.
  • Next, we make use of the map array method to loop through the data, restructured the data by making use of curly brackets{}, and then extract the todosas well as theid.
  • Finally, we called the keyattribute in React, pass in  id so as to enable React to track the data loaded on the page. The static text beside the &nbsp; also gets cleared before replacing it with the todo data.

We can now proceed to save our changes.

Deleting Data from the Firestore in React

To implement the delete functionality, we need to import two Firestore functions which are doc, deleteDoc.

JavaScript

 

import { collection, addDoc, serverTimestamp, getDocs, doc, deleteDoc } from 'firebase/firestore'

Next, we create a function called deleteTodo:

JavaScript

 

{     try {       if (window.confirm("Are you sure you want to delete this Task!"))     {     const documentRef = doc(db, "todo", id);     await deleteDoc(documentRef)     window.location.reload()     }     } catch (err) {       console.log(err);     } }” data-lang=”text/javascript”>

//Delete Handler const deleteTodo = async (id) => {     try {       if (window.confirm("Are you sure you want to delete this Task!"))     {     const documentRef = doc(db, "todo", id);     await deleteDoc(documentRef)     window.location.reload()     }     } catch (err) {       console.log(err);     } }

  • Within the try block, we start by displaying a prompt to the user if they want to proceed to delete the Todo.
  • We then create a new variable called documentRef. We then call the doc method which requires 3 arguments which are the firebase service, the collection name as well as the todo.id that we want to delete.
  • Next, we call the deleteDoc method from Firestore and then pass it in the documentRefas an argument. This will enable the specific todo to be deleted from the database. Once this is done, we refresh the page by calling the window.location.reload() function.
  • We then use the catchblock to handle any possible error by login into the console.

Now that our delete function is ready, all we have to do is to initialize it inside our delete button as seen below:

<div class="codeMirror-code–wrapper" data-code="” data-lang=”text/html”>

<button    type="button"    className="btn btn-danger float-end"    onClick={() => deleteTodo(id)}    >Delete </button>