Giter VIP home page Giter VIP logo

reactjs-template's Introduction

Packages

Inspiration

Docs

Firebase helpers
  • Add Data
import { collection, addDoc } from 'firebase/firestore';

try {
  const docRef = await addDoc(collection(db, 'users'), {
    first: 'Ada',
    last: 'Lovelace',
    born: 1815,
  });
  console.log('Document written with ID: ', docRef.id);
} catch (e) {
  console.error('Error adding document: ', e);
}
  • Read Data
import { collection, getDocs } from 'firebase/firestore';

const querySnapshot = await getDocs(collection(db, 'users'));
querySnapshot.forEach((doc) => {
  console.log(`${doc.id} => ${doc.data()}`);
});
  • To create or overwrite a single document
    • When you use set() to create a document, you must specify an ID for the document to create.
import { doc, setDoc } from 'firebase/firestore';

// Add a new document in collection "cities"
await setDoc(doc(db, 'cities', 'LA'), {
  name: 'Los Angeles',
  state: 'CA',
  country: 'USA',
});

await setDoc(doc(db, 'cities', 'new-city-id'), data);
  • Update a document
import { doc, updateDoc } from 'firebase/firestore';

const washingtonRef = doc(db, 'cities', 'DC');

// Set the "capital" field of the city 'DC'
await updateDoc(washingtonRef, {
  capital: true,
});
  • Server Timestamp
import { updateDoc, serverTimestamp } from 'firebase/firestore';

const docRef = doc(db, 'objects', 'some-id');

// Update the timestamp field with the value from the server
const updateTimestamp = await updateDoc(docRef, {
  timestamp: serverTimestamp(),
});
  • Get a Document Once
import { doc, getDoc } from 'firebase/firestore';

const docRef = doc(db, 'cities', 'SF');
const docSnap = await getDoc(docRef);

if (docSnap.exists()) {
  console.log('Document data:', docSnap.data());
} else {
  // doc.data() will be undefined in this case
  console.log('No such document!');
}
  • Realtime updates
import { doc, onSnapshot } from 'firebase/firestore';

const unsub = onSnapshot(doc(db, 'cities', 'SF'), (doc) => {
  console.log('Current data: ', doc.data());
});

reactjs-template's People

Contributors

yuvaraj2111 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.