{"id":5249,"date":"2025-04-24T05:32:35","date_gmt":"2025-04-24T05:32:34","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=5249"},"modified":"2025-04-24T05:32:35","modified_gmt":"2025-04-24T05:32:34","slug":"build-a-notes-app-in-react-with-firebase","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/build-a-notes-app-in-react-with-firebase\/","title":{"rendered":"Build a Notes App in React with Firebase"},"content":{"rendered":"<h1>Building a Notes App in React with Firebase<\/h1>\n<p>Creating a notes application provides an excellent learning opportunity for developers looking to enhance their skills in React and Firebase. This tutorial will guide you through building a simple notes app that allows users to create, read, update, and delete notes efficiently using Firebase as the backend.<\/p>\n<h2>Table of Contents<\/h2>\n<ul>\n<li><a href=\"#prerequisites\">Prerequisites<\/a><\/li>\n<li><a href=\"#setting-up-firebase\">Setting Up Firebase<\/a><\/li>\n<li><a href=\"#creating-a-react-app\">Creating a React App<\/a><\/li>\n<li><a href=\"#installing-firebase\">Installing Firebase<\/a><\/li>\n<li><a href=\"#implementing-the-notes-app\">Implementing the Notes App<\/a><\/li>\n<li><a href=\"#styling-the-app\">Styling the App<\/a><\/li>\n<li><a href=\"#final-thoughts\">Final Thoughts<\/a><\/li>\n<\/ul>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<p>Before you begin following this tutorial, ensure that you have:<\/p>\n<ul>\n<li>Basic knowledge of JavaScript and React<\/li>\n<li>An active Firebase account<\/li>\n<li>Node.js and npm installed on your machine<\/li>\n<\/ul>\n<h2 id=\"setting-up-firebase\">Setting Up Firebase<\/h2>\n<p>To use Firebase in our application, we need to set up a Firebase project:<\/p>\n<ol>\n<li>Go to the <strong><a href=\"https:\/\/console.firebase.google.com\/\">Firebase Console<\/a><\/strong>.<\/li>\n<li>Create a new project by clicking on &#8220;Add Project&#8221;.<\/li>\n<li>Follow the steps, and when prompted, enable Firestore Database.<\/li>\n<li>Once the project is created, go to the project settings and scroll down to \u201cYour apps\u201d section.<\/li>\n<li>Add a new Web app by clicking on the <\/strong><em>&#8220;Add app&#8221;<\/em><\/strong> icon.<\/li>\n<li>Copy the Firebase SDK configuration object as we will need it in our React app.<\/li>\n<\/ol>\n<h2 id=\"creating-a-react-app\">Creating a React App<\/h2>\n<p>For our notes app, we will create a new React application using Create React App.<\/p>\n<pre><code>npx create-react-app notes-app<\/code><\/pre>\n<p>Navigate to your application directory:<\/p>\n<pre><code>cd notes-app<\/code><\/pre>\n<h2 id=\"installing-firebase\">Installing Firebase<\/h2>\n<p>Once you&#8217;ve created your React app, install Firebase by running:<\/p>\n<pre><code>npm install firebase<\/code><\/pre>\n<h2 id=\"implementing-the-notes-app\">Implementing the Notes App<\/h2>\n<p>Now, let&#8217;s implement our notes functionality in a structured manner.<\/p>\n<h3>Creating the Firebase Configuration<\/h3>\n<p>Create a new file named <strong>firebase-config.js<\/strong> in the <strong>src<\/strong> folder and add the following code using your Firebase configuration:<\/p>\n<pre><code>import { initializeApp } from \"firebase\/app\";\nimport { getFirestore } from \"firebase\/firestore\";\n\nconst firebaseConfig = {\n  apiKey: \"YOUR_API_KEY\",\n  authDomain: \"YOUR_PROJECT_ID.firebaseapp.com\",\n  projectId: \"YOUR_PROJECT_ID\",\n  storageBucket: \"YOUR_PROJECT_ID.appspot.com\",\n  messagingSenderId: \"YOUR_MESSAGING_SENDER_ID\",\n  appId: \"YOUR_APP_ID\"\n};\n\n\/\/ Initialize Firebase\nconst app = initializeApp(firebaseConfig);\nconst db = getFirestore(app);\n\nexport { db };<\/code><\/pre>\n<h3>Setting Up Firestore<\/h3>\n<p>To keep our notes, we will leverage Firestore. Each note will have a title and content. Update your main app component to include Firestore functionality.<\/p>\n<pre><code>import React, { useEffect, useState } from 'react';\nimport { db } from '.\/firebase-config';\nimport { collection, addDoc, onSnapshot, deleteDoc, doc } from 'firebase\/firestore';\n\nconst App = () =&gt; {\n  const [notes, setNotes] = useState([]);\n  const [title, setTitle] = useState('');\n  const [content, setContent] = useState('');\n\n  const notesCollectionRef = collection(db, 'notes');\n\n  const createNote = async(e) =&gt; {\n    e.preventDefault();\n    await addDoc(notesCollectionRef, { title, content });\n    setTitle('');\n    setContent('');\n  };\n\n  useEffect(() =&gt; {\n    const unsubscribe = onSnapshot(notesCollectionRef, (snapshot) =&gt; {\n      setNotes(snapshot.docs.map(doc =&gt; ({ ...doc.data(), id: doc.id })));\n    });\n\n    return () =&gt; unsubscribe();\n  }, []);\n\n  const deleteNote = async(id) =&gt; {\n    const noteDoc = doc(db, 'notes', id);\n    await deleteDoc(noteDoc);\n  };\n\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Notes App&lt;\/h1&gt;\n      &lt;form onSubmit={createNote}&gt;\n        &lt;input type=\"text\" placeholder=\"Title\" value={title} onChange={(e) =&gt; setTitle(e.target.value)} \/&gt;\n        &lt;textarea placeholder=\"Content\" value={content} onChange={(e) =&gt; setContent(e.target.value)}&gt;&lt;\/textarea&gt;\n        &lt;button type=\"submit\"&gt;Add Note&lt;\/button&gt;\n      &lt;\/form&gt;\n      &lt;ul&gt;\n        {notes.map(note =&gt; (\n          &lt;li key={note.id}&gt;\n            &lt;h2&gt;{note.title}&lt;\/h2&gt;\n            &lt;p&gt;{note.content}&lt;\/p&gt;\n            &lt;button onClick={() =&gt; deleteNote(note.id)}&gt;Delete&lt;\/button&gt;\n          &lt;\/li&gt;\n        ))}\n      &lt;\/ul&gt;\n    &lt;\/div&gt;\n  );\n};\n\nexport default App;<\/code><\/pre>\n<h3>Understanding the Code<\/h3>\n<p>Let&#8217;s dissect what we have done:<\/p>\n<ul>\n<li>We set up state variables for notes, title, and content.<\/li>\n<li>We created a function, <strong>createNote<\/strong>, that adds a new document in our Firestore collection when a note is submitted.<\/li>\n<li>We set up a real-time listener using <strong>onSnapshot<\/strong> to retrieve notes from Firestore and update our state.<\/li>\n<li>A <strong>deleteNote<\/strong> function allows users to remove notes.<\/li>\n<li>The JSX returns a form for creating notes and a list that displays each note along with a delete button.<\/li>\n<\/ul>\n<h2 id=\"styling-the-app\">Styling the App<\/h2>\n<p>To make our notes app visually appealing, we can add some simple CSS styles. Create a file named <strong>App.css<\/strong> in the <strong>src<\/strong> directory:<\/p>\n<pre><code>h1 {\n  text-align: center;\n}\n\nform {\n  margin: 20px;\n}\n\ninput, textarea {\n  width: calc(100% - 20px);\n  margin: 5px;\n  padding: 10px;\n}\n\nbutton {\n  padding: 10px;\n}\n\nul {\n  list-style-type: none;\n}\n\nli {\n  border: 1px solid #ccc;\n  margin: 10px;\n  padding: 10px;\n}\n<\/code><\/pre>\n<p>Import the CSS file in your <strong>App.js<\/strong> file:<\/p>\n<pre><code>import '.\/App.css';<\/code><\/pre>\n<h2 id=\"final-thoughts\">Final Thoughts<\/h2>\n<p>Congratulations! You have successfully built a notes application using React and Firebase. This project not only enhances your React skills but also provides experience with Firebase, a powerful and flexible cloud-based solution for your app backend.<\/p>\n<p>Feel free to extend this project by adding features like:<\/p>\n<ul>\n<li>User authentication using Firebase Authentication<\/li>\n<li>Editing existing notes<\/li>\n<li>Searching for notes<\/li>\n<\/ul>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Building a Notes App in React with Firebase Creating a notes application provides an excellent learning opportunity for developers looking to enhance their skills in React and Firebase. This tutorial will guide you through building a simple notes app that allows users to create, read, update, and delete notes efficiently using Firebase as the backend.<\/p>\n","protected":false},"author":100,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[398],"tags":[224],"class_list":{"0":"post-5249","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-react","7":"tag-react"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/5249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=5249"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/5249\/revisions"}],"predecessor-version":[{"id":5250,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/5249\/revisions\/5250"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=5249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=5249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=5249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}