Build a Google Docs Clone with React and Firebase

Dedsec

Pedro Machado / November 17, 2024

4 min read •

Description

Learn how to create a real-time collaborative text editor inspired by Google Docs using React and Firebase.

Build a Google Docs Clone Tutorial

In this guide, you’ll learn how to create a real-time collaborative text editor inspired by Google Docs. We’ll use React, Firebase, and React Quill to build a sleek, modern editor with live updates and dynamic synchronization.


Watch the Full Tutorial

Prefer to watch the tutorial in video format? Check out the full walkthrough on my YouTube channel:

Visit my channel for more React and Firebase tutorials: PedroTech YouTube Channel.


What You’ll Learn

  • Build a real-time collaborative text editor with React and Firebase.
  • Implement Firestore for real-time data synchronization.
  • Customize the editor to match the look and feel of Google Docs.
  • Optimize performance and reduce database calls with session-based caching.

Prerequisites

Before starting, make sure you have the following tools installed:

  • Node.js (v14 or later)
  • npm or yarn
  • Basic knowledge of JavaScript, React, and Firebase

Step 1: Set Up Your Project

Create a new React project using Vite:

npm create vite@latest google-docs-clone --template react-ts
cd google-docs-clone

Install the required dependencies:

npm install react-quill firebase lodash

Step 2: Configure Firebase

Set up Firebase for real-time data synchronization:

  1. Create a new project in the Firebase Console.
  2. Enable Firestore in the Firebase project settings.
  3. Add a web app to the project and copy the Firebase config.

Create a new file, firebase-config.ts, in your project and add the Firebase configuration:

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
 
const firebaseConfig = {
  apiKey: "YOUR_API_KEY",
  authDomain: "YOUR_AUTH_DOMAIN",
  projectId: "YOUR_PROJECT_ID",
  storageBucket: "YOUR_STORAGE_BUCKET",
  messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
  appId: "YOUR_APP_ID",
};
 
const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);

Step 3: Create the Editor Component

Set up the main editor component using React Quill:

import React, { useEffect, useRef, useState } from "react";
import ReactQuill from "react-quill";
import { db } from "../firebase-config";
import { doc, getDoc, setDoc, onSnapshot } from "firebase/firestore";
import "react-quill/dist/quill.snow.css";
 
function Editor() {
  const [editorContent, setEditorContent] = useState<string>("");
  const quillRef = useRef<any>(null);
  const documentRef = doc(db, "documents", "example-doc");
 
  useEffect(() => {
    const loadContent = async () => {
      const docSnap = await getDoc(documentRef);
      if (docSnap.exists()) {
        const savedContent = docSnap.data().content;
        setEditorContent(savedContent);
      }
    };
 
    loadContent();
 
    const unsubscribe = onSnapshot(documentRef, (snapshot) => {
      if (snapshot.exists()) {
        const newContent = snapshot.data().content;
        setEditorContent(newContent);
      }
    });
 
    return () => unsubscribe();
  }, []);
 
  const handleEditorChange = (value: string) => {
    setEditorContent(value);
    setDoc(documentRef, { content: value }, { merge: true });
  };
 
  return (
    <div className="google-docs-editor">
      <ReactQuill
        ref={quillRef}
        value={editorContent}
        onChange={handleEditorChange}
      />
    </div>
  );
}
 
export default Editor;

Step 4: Style the Editor

To make the editor resemble Google Docs, apply the following styles in your App.css file:

.google-docs-editor .ql-container {
  width: 210mm;
  height: 297mm;
  border: 1px solid #ddd;
  margin: auto;
  padding: 20px;
  box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
  background-color: #fff;
}

Step 5: Run the Development Server

Start the development server and test your project:

npm run dev

Visit http://localhost:3000 to see your collaborative text editor in action.


Additional Features

Add Real-Time Collaboration

To enable real-time collaboration, we used Firebase Firestore’s onSnapshot method, which listens for updates and syncs the content across all clients dynamically.

Optimize with Caching

To reduce database calls, we implemented caching using sessionStorage. This ensures the editor loads faster for returning users by storing the document locally in the browser.


Additional Resources


By following this guide, you’ve created a powerful, real-time collaborative text editor with React and Firebase. Customize it further to add even more features and make it your own!