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:
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:
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
Can you use a datatable for fetching the data in firebase? Because when I use it even though the data is in the table when you search something it will always show no data available in the table and below of it it show 0 entries.
Please check this and this will help to get dynamic data:
https://therichpost.com/reactjs-datatable-with-export-buttons-print-csv-copy-with-dynamic-data/