From ffa951936e097e1ec08ee534b8f0778337d6fb8a Mon Sep 17 00:00:00 2001 From: "Rayyan (Rayy)" Date: Tue, 17 Oct 2023 09:21:08 +0000 Subject: [PATCH] Fetch data from Google Firestore --- src/App.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/App.js b/src/App.js index 3171cb8..f585876 100644 --- a/src/App.js +++ b/src/App.js @@ -1,7 +1,7 @@ import { FirebaseConfig } from "./config/Config" import { initializeApp } from "firebase/app" import { Routes, Route } from "react-router-dom" -import { useState } from "react" +import { useState, useEffect } from "react" import { getAuth, createUserWithEmailAndPassword, onAuthStateChanged, @@ -52,6 +52,15 @@ function App() { /// application states const [nav, setNav] = useState(navItems) const [auth, setAuth] = useState(false) + const [data, setData] = useState([]) + const [ fetching , setFetching ] = useState( false ) + + useEffect( () => { + if( data.length === 0 ) { + readData() + setFetching( true ) + } + }, [data]) // authentication observer @@ -106,12 +115,13 @@ const signIn = (email, password) => { // function to get data const readData = async () => { const querySnapshot = await getDocs( collection(FBdb, "books") ) - let data = [] + let listdata = [] querySnapshot.forEach( (doc) => { let item = doc.data() item.id = doc.id - data.push( item ) + listdata.push( item ) }) + setData( listdata ) } @@ -120,7 +130,7 @@ const readData = async () => {
- } /> + } /> } /> } /> } />