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.
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.
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:
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:
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 theclassName
of App and then import the Todo.js component as seen in the following lines of code:
<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:
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:
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:
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:
We can now grab this and paste it inside our firebase.config.js file:
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:
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:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">