Monday, May 20, 2024

How to fetch data from firebase in reactjs?

How to fetch data from firebase in reactjs?

Hello friends, welcome again on my blog therichpost. Today in this blog post, I am going to tell you, How to fetch data from firebase in reactjs?

Here are the videos for better understanding of working:

Reactjs with Firebase data
How to create database on Firebase?

Post Working:

Friends in this post, I am getting data from firebase backend into my reactjs application. This is very easy friends. I have also used bootstrap for better styling.

For Reactjs new comers, please check below link:

Reactjs Basic

Here is the code snippet for How to fetch data from firebase in reactjs?and please use carefully to avoid mistakes:

1. Firstly friends we need fresh react set up and for this, we need to run below commands into our terminal or if you already have then no need for this step. Importantly we should have latest node version installed on our system:

npx create-react-app therichpost

cd therichpost

npm start //this will make project run on browser http://localhost:3000/

2. Now friends we need to run below commands into our project terminal to get firebase and others modules which will help us to achieve this post working:

npm install --save firebase

npm install bootstrap --save //optional

3. Now friends we need to create new file “Firebase.js” inside project/src folder and after creating the file add below code inside project/src/Firebase.js file:

import * as firebase from 'firebase';

const config = {
    apiKey: "****",
    authDomain: "*****",
    databaseURL: "****",
    projectId: "****",
    storageBucket: "****",
    messagingSenderId: "****",
    appId: "****",
    measurementId: "****"


export default firebase;

4. Finally friends we just need to add below code inside our project/src/App.js file to get final output on browser:

import React from 'react';

import './App.css';

//Calling Bootstrap 4.5 css
import 'bootstrap/dist/css/bootstrap.min.css';

//Calling Firebase config setting to call the data
import firebase from './Firebase';

class App extends React.Component {

constructor(props) {
    this.state = {studentslist : []}
  componentDidMount() {
      firebase.database().ref("students-list").on("value", snapshot => {
        let studentlist = [];
        snapshot.forEach(snap => {
            // snap.val() is the dictionary with all your keys/values from the 'students-list' path
        this.setState({ studentslist: studentlist });
  return (
    <div className="MainDiv">
      <div class="jumbotron text-center bg-sky">
          <h3>How to show firebase data in reactjs?</h3>
      <div className="container">
          <table id="example" class="display table">
            <thead class="thead-dark">
            { => {
                return (
export default App;

Now we are done friends. If you have any kind of query or suggestion or any requirement then feel free to comment below.

Note: Friends, I just tell the basic setup and things, you can change the code according to your requirements.
I will appreciate that if you will tell your views for this post.Nothing matters if your views will good or bad.



the authortherichpost
Hello to all. Welcome to Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.