Open In App

Fetch and Send with Firestore using ReactJS

Last Updated : 07 Jun, 2024
Comments
Improve
Suggest changes
4 Likes
Like
Report

To perform fetch and send with Firestore using React require sending and receving data on the firestore database. 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.

Prerequisites:

Approach

To perform fetch and send with Firestore using React first set up the firebase project configuration in the react application. create a database in the firestore and link a form to send data. Fetch this data form firestore using the collection method and display on the web page

Step to Create React Application and Installing Firebase

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

npx create-react-app gfgfirestore

Step 2: Move to the project directory

cd gfgfirestore

Project Structure:

The project structure will look like this.

Screenshot-from-2023-10-31-14-07-40

Step 3: After creating the ReactJS application, Install the firebase module using the following command:

npm i --save firebase@8.3.1 

Dependencies list after installing packages

{
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"firebase": "^8.3.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
}

Step 4: Go to your firebase dashboard and create a new project and copy your credentials.

const firebaseConfig = {
apiKey: "your api key",
authDomain: "your credentials",
projectId: "your credentials",
storageBucket: "your credentials",
messagingSenderId: "your credentials",
appId: "your credentials"
};

Example: Create a basic User Interface for adding data to the store and to read and display the data fetch from the firestore databse.

JavaScript
// Filename - App.js

import React from "react";
import "./App.css";
import Read from "./Read.js";
import Firestore from "./firestore";
function App() {
    return (
        <div
            style={{ textAlign: "center", margin: "auto" }}
        >
            <h1 style={{ color: "green" }}>
                GeeksforGeeks
            </h1>
            <h3>Form linked to firestore to send data</h3>
            <Firestore />
            <h3>Data fetch from firestore</h3>
            <Read />
        </div>
    );
}

export default App;
JavaScript JavaScript JavaScript

Step to run the application: Use this command in the terminal in project directory.

npm start

Output: This output screens will be visible on the browser.

  • Submitting the form after filling in the details to add the data

  • The view of the data that is added to the store in firebase
  • Records that are present in the database are displayed in our application.



Next Article

Similar Reads