npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@camkerber/react-firebase-db

v1.0.2

Published

React provider for Firebase Realtime Database

Downloads

27

Readme

react-firebase-db

The purpose of this package is to expedite the set up of Firebase Realtime Database for React projects. There are additional configuration options available that allow for anonymous authorization and app check through reCaptcha Enterprise.

This documentation assumes you have already created a Firebase project and registered your app. You can find the documentation for these steps here.

FirebaseProvider

This is intended to wrap your app at the root level or as the parent in your component tree where you plan on using the context values returned by the provider.

Props

options

  • The object passed to the options prop is your Firebase configuration. This must be generated directly from your Firebase project. You can find documentation on how to generate it at this link.

withAnonymousAuth

  • Firebase gives developers the opportunity to have a small layer of security for apps that don't require an account to fetch data. You can follow the documentation here to set up this feature for your product, then pass true to this prop use the protection in your app.

recaptchaSiteKey

  • If you are using App Check through reCaptcha Enterprise, this prop is where should pass your site key. There is further documentation on setting up App Check here.

assignAppCheckDebugToken

This function is specifically for apps using Firebase App Check. You should call this function outside of your app's root component. A reasonable place is above where you execute createRoot

useFirebaseContext

This hook returns a Firebase Realtime Database reference, dbRef, that can be used to query Firebase in whatever way you prefer. However, if you are using App Check or Anonymous Auth, be certain that the returned value for initializing is false, otherwise you will encounter authorization errors.

Firebase documentation for fetching data with a Database Reference can be found here.

Example Implementation

Simple demonstration of how this library can be used.

main.tsx

import {assignAppCheckDebugToken, FirebaseProvider} from "@camkerber/react-firebase-db";
import {FirebaseOptions} from "firebase/app";
import {createRoot} from "react-dom/client";

const firebaseOptions: FirebaseOptions = {
  apiKey: "your-api-key",
  authDomain: "example.com",
  projectId: "your-project-name",
  storageBucket: "example.com",
  messagingSenderId: "exampleId",
  appId: "exampleId",
  measurementId: "exampleId",
};

const RECAPTCHA_SITE_KEY = "your-site-key";

const FIREBASE_APPCHECK_DEBUG_TOKEN = "your-debug-token";

assignAppCheckDebugToken(FIREBASE_APPCHECK_DEBUG_TOKEN);

createRoot(document.getElementById("root")!).render(
  <FirebaseProvider
    options={firebaseOptions}
    withAnonymousAuth
    recaptchaSiteKey={RECAPTCHA_SITE_KEY}
  >
    <YourApp />
  </FirebaseProvider>
);

YourApp.tsx

import {get} from "firebase/database";
import {useFirebaseContext} from "@camkerber/react-firebase-db";
import {CircularProgress} from "@mui/material";

const App = () => {
  const {dbRef, initializing} = useFirebaseContext();

  const handleGetData = async () => {
    const snapshot = await get(dbRef);
    if (snapshot.exists()) {
      return snapshot.val();
    }
  };

  return (
    <>
      {initializing ? (
        <CircularProgress />
      ) : (
        <button onClick={handleGetData}>Get Data</button>
      )}
    </>
  );
};