An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
October 17, 2021 04:20 pm GMT
Original Link: https://dev.to/karthik_n/read-and-write-using-firebaserealtime-database-in-react-js-2m1
Read and Write using firebase(realtime database) in React JS
Hi everyone,
here i am going to show you how to use firebase in react js
First install firebase
npm i firebase
create config file
import "firebase/auth";import "firebase/database";import { initializeApp } from 'firebase/app';import { getDatabase } from "firebase/database";const firebaseConfig = { apiKey: "YOUR_API_KEY", authDomain: "YOUT_AUTH_DOMAIN", databaseURL: "YOUR_DATABASE_URL", projectId: "YOUR_PROJECT_ID", storageBucket: "YOUR_STORAGE_BUCKET", messagingSenderId: "YOUR_MESSENGER_SENDER_ID", appId: "YOUR_APP_ID"};// Initialize Firebaseexport const app = initializeApp(firebaseConfig);export const database = getDatabase(app);
if you don't know how to download this config go to this link Firebase web-setup
function for store new data
function writeData() { const db = getDatabase(); const postListRef = ref(db, 'users/'); // const newPostRef = push(postListRef); set(newPostRef, { username: item, }); }
function for delete data
const Deletedata = (e) => { const db = getDatabase(); remove(ref(db, `users/${e.key}`)); }
code for fetch data
const Ref = ref(database, 'users/',); useEffect(() => { onValue(Ref, (snapshot) => { const data = snapshot.val(); }); }, [])
Original Link: https://dev.to/karthik_n/read-and-write-using-firebaserealtime-database-in-react-js-2m1
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To