An open API service indexing awesome lists of open source software.

https://github.com/muhdhanish/firebase-integration

This is a simple guide for basic crud in firebase using React js.
https://github.com/muhdhanish/firebase-integration

context-api css firebase javascript reactjs

Last synced: 3 months ago
JSON representation

This is a simple guide for basic crud in firebase using React js.

Awesome Lists containing this project

README

          

# Firebase Integration with React

This repository serves as a demonstration of how to integrate Firebase with React using the latest version of Firebase. It includes a utility function useFirestoreCollection that facilitates interactions with Firestore collections.

### Step 1: Clone the repository

```bash
git clone https://github.com/MuhdHanish/Firebase-integration.git
```

### Step 2: Install dependencies

```bash
npm install
```

### Step 3: Set up Firebase

- Create a Firebase project in the Firebase Console.
- Obtain your Firebase configuration object.
- Add the Firebase configuration object to src/firebase/config.js.

# Usage

```javascript
import {
collection,
getDocs,
addDoc,
updateDoc,
doc,
getDoc,
deleteDoc,
} from "firebase/firestore";
import db from "./firebase/config";

export function useFirestoreCollection(collectionName) {
// Initialize collection reference
const collectionRef = collection(db, collectionName);

// Get all documents from the collection
const getDocuments = async () => {
const data = await getDocs(collectionRef);
return data.docs.map((doc) => ({ ...doc.data(), id: doc.id }));
};

// Add a document to the collection
const addDocument = async (data) => {
try {
await addDoc(collectionRef, data);
} catch (error) {
console.error("Error adding document:", error);
}
};

// Update a document in the collection
const updateDocument = async (documentId) => {
const documentRef = doc(db, collectionName, documentId);
const documentSnap = await getDoc(documentRef);
const data = {
age: documentSnap.data().age + 1,
};
if (documentSnap.exists()) {
await updateDoc(documentRef, data);
}
};

// Delete a document from the collection
const deleteDocument = async (documentId) => {
await deleteDoc(doc(db, collectionName, documentId));
};

return { getDocuments, addDocument, updateDocument, deleteDocument };
}
```

# Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or create a pull request.