Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 17, 2021 04:20 pm GMT

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,    });  }

Image description

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To