Skip to content
Related Articles

Related Articles

How to perform fetch and send with Firestore using ReactJS ?

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Apr, 2021

In this article, we will learn how to fetch data from Firestore and display it inside a React application. Firestore is a NoSQL database developed by Google as an alternative to the firebase database. It has been designed to provide a better developer experience and simplify the development process. The below steps demonstrate the process of creating the application.

Step 1: Create a new React application. We use create-react-app to create our application.

npx create-react-app gfgfirestore

Step 2: Install the firebase package in the project using npm.

npm install firebase@8.3.1 --save

Step 3: Create a new project from the Firebase dashboard by filling in the necessary details and check the format of the data that is stored in Firestore. In this example, we have an object in which name, age, and courseEnrolled are the fields in which data is stored.

Step 4: Initialize Firebase in the project by copying the relevant credentials from the Firebase dashboard.


import firebase from 'firebase';
var firebaseConfig = {
    // Firebase credentials
// Initialize Firebase
var db = firebase.firestore();
export default db;

Step 5: Create a basic User Interface for adding data to the store.


import db from './firbase';
import {useState} from 'react';
const Firestore = () => {
    const [name  , Setname] = useState("");
    const [age , Setage] = useState("");
    const [course , Setcourse] = useState("");
    const sub = (e) => {
        // Add data to the store
            Nane: name,
            Age: age,
            CourseEnrolled: course
        .then((docRef) => {
            alert("Data Successfully Submitted");
        .catch((error) => {
            console.error("Error adding document: ", error);
    return (
                <form style={{marginTop:"200px" }}
                  onSubmit={(event) => {sub(event)}}>
                    <input type="text" placeholder="your name"
                      onChange={(e)=>{Setname(}} />
                    <input type="number" placeholder="your age"
                    <input type="text" placeholder="Course Enrolled"
                    <button type="submit">Submit</button>
export default Firestore;


  • Submitting the form after filling in the details

  • The view of the data that is added to the store

Step 6: Create a basic User Interface for showing the store data. We are going to use the get() method for fetching the data from the store. The data is then looped through and used in the 


// Import Firestore database
import db from './firbase';
import { useState } from 'react';
import './read.css';
const Read = () => {
    const [info , setInfo] = useState([]);
    // Start the fetch operation as soon as
    // the page loads
    window.addEventListener('load', () => {
    // Fetch the required data using the get() method
    const Fetchdata = ()=>{
        db.collection("data").get().then((querySnapshot) => {
            // Loop through the data and store
            // it in array to display
            querySnapshot.forEach(element => {
                var data =;
                setInfo(arr => [...arr , data]);
    // Display the result on the page
    return (
            <h2>Student Details</h2>
   => (
            <Frame course={data.CourseEnrolled} 
// Define how each display entry will be structured
const Frame = ({course , name , age}) => {
    console.log(course + " " + name + " " + age);
    return (
            <div className="div">
<p>NAME : {name}</p>
<p>Age : {age}</p>
<p>Course : {course}</p>
export default Read;

Output: In this example, four records that are present in the database are displayed in our application.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!