Weekend Project: Time Tracker Using ReactJS and Firebase
In this weekend project, we will be building a Time Tracker application using ReactJS and Firebase. The Time Tracker will allow users to log their activities and track the time spent on each task. Firebase will be used as the backend to store the data and provide real-time synchronization. This project is a great opportunity to learn and apply ReactJS concepts along with integrating a powerful backend solution like Firebase.
Prerequisites: To follow along with this project, you should have a basic understanding of HTML, CSS, and JavaScript. Familiarity with ReactJS and Firebase will be helpful but not mandatory. Make sure you have Node.js and npm (Node Package Manager) installed on your machine.
Setting up the Project:
- Create a new ReactJS project using Create React App. Open your terminal and run the following command:lua
npx create-react-app time-tracker
- Navigate to the project directory:bash
cd time-tracker
- Install the Firebase SDK by running the following command:
npm install firebase
Setting up Firebase:
- Go to the Firebase console (https://console.firebase.google.com/) and create a new project.
- Once the project is created, click on "Add app" and select the web platform.
- Register your app by providing a name for your web app.
- Firebase will generate a configuration object with your app's credentials. Copy this configuration object.
Connecting Firebase to the React App:
- In your React project, open the
src
directory and create a new file calledfirebase.js
. - Paste the copied configuration object into the
firebase.js
file. - Import the necessary Firebase modules and initialize Firebase in the file. Your
firebase.js
file should look like this:
javascriptimport firebase from 'firebase/app';
import 'firebase/firestore';
const firebaseConfig = {
// Your Firebase configuration object
};
firebase.initializeApp(firebaseConfig);
export default firebase;
Building the Time Tracker:
- In the
src
directory, create a new directory calledcomponents
. - Inside the
components
directory, create a new file calledTimeTracker.js
. - Open the
TimeTracker.js
file and import React and the Firebase module you installed earlier. - Create a functional component called
TimeTracker
and export it as the default component. - Within the
TimeTracker
component, initialize a Firestore reference to the collection you'll use to store the time entries. You can do this by adding the following code:
javascriptconst db = firebase.firestore();
const timeEntriesRef = db.collection('timeEntries');
- Next, create a form within the
TimeTracker
component to allow users to log their activities. The form should include fields for the task name and the time spent. - Add an
onChange
event handler to update the component's state whenever the form fields change. - Add an
onSubmit
event handler to handle the form submission. Inside the event handler, use thetimeEntriesRef
to add a new document to the collection with the submitted data. - Retrieve the time entries from Firebase and display them in a list below the form. You can use the Firestore
onSnapshot
method to listen for real-time updates to the collection and update the component's state accordingly. - Style the Time Tracker application using CSS to make it visually appealing.