mrwho
mrwho

Reputation: 67

Retrieve Data data once time Firebase instead of onSnapshot

I'm trying to retrieve data once time instead to using onSnapshot.

    import { onSnapshot, collection  } from 'firebase/firestore';

function App() {
  const [books , setbooks] = useState([]);
  console.log(books);
  
     useEffect(() => {
       onSnapshot(collection(db,'books'),(snapshot)=>{
         setbooks(snapshot.docs.map((doc) =>({...doc.data(), id: doc.id})));
       })
       
      },[]);

Upvotes: 0

Views: 373

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598708

The equivalent while getting the data once would be:

useEffect(() => {
  getDocs(collection(db,'books')).then((snapshot)=>{
    setbooks(snapshot.docs.map((doc) =>({...doc.data(), id: doc.id})));
  })   
},[]);

Upvotes: 1

Related Questions