Categories

Thursday, November 21, 2024
#919814419350 therichposts@gmail.com
FirebaseReactjs

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: "****"

  
};
firebase.initializeApp(config);

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) {
    
    super(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
            studentlist.push(snap.val());
        });
        this.setState({ studentslist: studentlist });
      });
    
    
 }
  
  render(){
  return (
    <div className="MainDiv">
      <div class="jumbotron text-center bg-sky">
          <h3>How to show firebase data in reactjs?</h3>
      </div>
    
      <div className="container">
          <table id="example" class="display table">
            <thead class="thead-dark">
                <tr>
                    <th>FirstName</th>
                    <th>Lastname</th>
                    <th>Email</th>
                    <th>Mobile</th>
                </tr>
            </thead>
            <tbody>
            {this.state.studentslist.map(data => {
                
                return (
                    <tr>     
                    <td>{data.firstName}</td>
                    <td>{data.lastName}</td>
                    <td>{data.email}</td>
                    <td>{data.mobileNumber}</td>
                    </tr>
                    
                );
               
                })}
        
               
            </tbody>
            
         </table>
          
     </div>
    </div>
  );
}
}
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.

Jassa

Thanks

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

2 Comments

Leave a Reply

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