All Projects → julianburr → react-firebase-context

julianburr / react-firebase-context

Licence: MIT license
A basic set of components that help dealing with Firebase services

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to react-firebase-context

ChatApp
Chat app based on Firebase tools.
Stars: ✭ 88 (+114.63%)
Mutual labels:  firestore, firebase-services
full-stack-firebase
This course will introduce you to Firebase and grow your understanding of the platform until you're comfortable deploying an app to production.
Stars: ✭ 100 (+143.9%)
Mutual labels:  firestore
React-Netflix-Clone
A Fully Responsive clone of Netflix website built using React.JS as a Front-end & Firebase as a Back-end.
Stars: ✭ 91 (+121.95%)
Mutual labels:  firestore
flutter-auth-ui
flutter-auth-ui is an authentication library for flutter web applications. It uses Firebase auth as security as a service (SECaaS) provider. It implements UI to register user, validate email, sign in, sign out, restore password, access firestore..
Stars: ✭ 59 (+43.9%)
Mutual labels:  firestore
whatsapp-clone-react
Build a WhatsApp Clone with React JS and FireBase.
Stars: ✭ 38 (-7.32%)
Mutual labels:  firestore
AngularPos
A real-time, simple web Point of Sale system written with Angular 12, Firebase (Cloud Firestore), Bootstrap 4 and PrimeNg
Stars: ✭ 67 (+63.41%)
Mutual labels:  firestore
firestore-to-bigquery-export
NPM package for copying and converting Cloud Firestore data to BigQuery.
Stars: ✭ 26 (-36.59%)
Mutual labels:  firestore
vue-js-3-firebase-firestore
Vue 3 Firebase Tutorial: Build Firestore CRUD Web Application
Stars: ✭ 34 (-17.07%)
Mutual labels:  firestore
go-firestorm
Simple Go ORM for Google/Firebase Cloud Firestore
Stars: ✭ 39 (-4.88%)
Mutual labels:  firestore
flutter-app
Full Feature Todos Flutter Mobile app with fireStore integration.
Stars: ✭ 138 (+236.59%)
Mutual labels:  firestore
unishox js
JS Library for Guaranteed compression of Unicode short strings
Stars: ✭ 27 (-34.15%)
Mutual labels:  firestore
serverless-rest-api
Building RESTful Web APIs with Firebase Cloud Function, Firestore, Express and TypeScript
Stars: ✭ 103 (+151.22%)
Mutual labels:  firestore
firebase-bundle
A Symfony Bundle for the Firebase PHP Admin SDK
Stars: ✭ 112 (+173.17%)
Mutual labels:  firestore
squanchy-flutter
Flutter implementation of the Squanchy conference app
Stars: ✭ 56 (+36.59%)
Mutual labels:  firestore
vue-blog
Book blog
Stars: ✭ 31 (-24.39%)
Mutual labels:  firestore
Sub-Track
Flutter Application to keep track of Subscriptions
Stars: ✭ 31 (-24.39%)
Mutual labels:  firestore
orkan
A content management toolkit for building and managing dynamic React applications with ease.
Stars: ✭ 25 (-39.02%)
Mutual labels:  firestore
JewelCase
This is the source code for JewelCase, a sample app demonstrating how to use SwiftUI and Firebase together. This slide deck discusses the architecture of the app: https://www.slideshare.net/peterfriese/building-swiftui-apps-with-firebase
Stars: ✭ 42 (+2.44%)
Mutual labels:  firestore
demo-firebase-js
A simple Web application that demonstrates how the end-to-end encryption works. The application uses firebase as a backend service for authentication and chat messaging, and Virgil E3Kit SDK for end-to-end encryption.
Stars: ✭ 31 (-24.39%)
Mutual labels:  firestore
foundry-cli
Foundry makes the development of Firebase Functions fast by giving you an out-of-the-box working cloud environment for your development with an access to your production data. It's a CLI tool that gives you a continuous REPL-like feedback about your Firebase Functions.
Stars: ✭ 49 (+19.51%)
Mutual labels:  firestore

logo


react-firebase-context

npm license

This is an experimental package, which aims to make dealing with Google Firebase services easier in React apps, espacially the data flow side of things.

Since its completely experimental, I decided to work the new React Suspense feature into the data flow handling, which seems to be the right way to go. That being said, besides the obvious warning that this should NOT be used in production apps, also be warned here that this will (currently) only work with the alpha build of React (yarn add react@canary react-dom@canary) due to those experimental features.

Why?

Dealing with data flows of any kind is always a huge pain in the a**. With this library I am trying to make it a bit simpler and less painful.

The general concept is to have everything Firebase related (or any of its services) stored and passed through to your components via the React Context API.

Install

yarn add react-firebase-context

# Or via npm
npm i react-firebase-context

Example

To make things easier to understand I am trying to build up a demo application with CRA as I go. This can be found in the example directory. Do the following to run it:

cd example
yarn && yarn start

Usage

The general setup to connect your app to your Firebase project is done by the FirebaseProvider, which takes all the config that you would usually pass to Firebase as props:

<FirebaseProvider 
  apiKey={apiKey}
  authDomain={authDomain}
  databaseURL={props.databaseURL}
  projectId={props.projectId}
  storageBucket={props.storageBucket}
  messagingSenderId={props.messagingSenderId}
>
  <App />
</FirebaseProvider>

This literally just runs firebase.initializeApp in the constructor, which allows you to access all the services Firebase offers. That also means that all service providers need to be placed within this FirebaseProvider. It's generally a good idea to store this sensitive information in your projects .env file or similar. With CRA you can then access them via process.env.REACT_APP_*.

Firestore

Firebase Documentation for Firestore

To set up the data context (which serves as cache) you need to use the FirestoreProvider. It takes all config arguments that you would usually pass into firestore.settings as props.

<FirebaseProvider {...config}>
  <FirestoreProvider>
    <App />
  </FirestoreProvider>
</FirebaseProvider>

Within your app you can then either use the HoC to get access to the Firestore instance ...

@withFirestore
class Example extends React.Component {
  componentDidMount () {
    this.props.firestore.firestore // = firestore instance
    this.props.firestore.data // = data cache
  }
}

… or use the Firestore component, which let's you define queries that it will automatically try to resolve.

<Firestore query={({firestore}) => firestore.collection('users')}>
  {({data, firestore}) => (
    <Fragment>
      <h1>Users</h1>
      <ul>
        {data.map(user => (
          <li key={user.id} onClick={() => firestore.collection('users').doc(user.id).delete()}>
            {user.data.name}
          </li>
        ))}
      </ul>
    </Fragment>
  )}
</Firestore>

In the core it will load the data from Firestore (using React Suspense to suspend the render until the data is loaded) and then store the data in the cache. It will keep listening to snapshot changes, so you'll always get the latest data. It will also, when the query is a list, store all individual items in the cache as well. So when an entity was in a list before, loading it invidually will be instant, no request to the Firebase server will be made 😊

Auth

Firebase Documentation for Authentication

Setup again through provider component, which initialises the listener for authentication changes.

<FirebaseProvider {...config}>
  <AuthProvider>
    <App />
  </AuthProvider>
</FirebaseProvider>

The actual auth data and functionality can then accessed via HoC ...

@withAuth
class Example extends React.Component {
  componentDidMount () {
    this.props.auth.getUserData // = get auth user data, or null if user is not logged in
    this.props.auth.loginWith* // = auth methods for different providers
    this.props.auth.logout // = logout method
  }
}

... or consumer component

<Suspense fallback={<p>Init app...</p>}>
  <Auth>
    {({getUserData, loginWithGoogle, logout}) => {
      const user = getUserData();
      return !user ? (
        <button onClick={loginWithGoogle}>
          Login with Google
        </button>
      ) : (
        <Fragment>
          <p>Hello {user.displayName}!</p>
          <button onClick={logout}>Logout</button>
      )}
    }
  </Auth>
</Suspense>

The auth consumer (component and HoC) will suspend its rendering until it gets the initial user data when using getUserData. By using Suspense you can decide where and how you want to handle that initial state.

Storage

Firebase Documentation for Storage

Same setup as for the other services. Use the storage provider wrapping everything in your app that needs access to the storage service functionality.

<FirbaseProvider {...config}>

</FirebaseProvider>

To get access to the storage functionality, you can use the HoC ...

@withStorage
class Example extends React.Component {
  componentDidMount () {
    this.props.storage // = firebase.storage()
  }
}

... or the consumer component

<Storage>
  {({storage}) => (
    <form ref={e => this.form = e}>
      <input
        type="file"
        onChange={e => {
          const file = e.target.files[0];
          if (file) {
            storage.ref().child(file.name).put(file).then(() => {
              this.form.reset();
            })
          }
        }}
      />
    </form>
  )}
</Storage>

Note that in the documentation it is recommended to keep information regarding your storage in a database structure (i.e. Firestore), since Firebase Storage does not provide an API to query or even list directories or files based on certain criteria (other than their full path).

Functions / ML Kit

Work in progress

Todo

  • Finish firestore components
  • Add auth components for easier authentication flows / access of authentication data via context provider and consumer
  • Add support for storage service
  • Allow defining auth provider settings (e.g. scopes) via props / args in the auth methods
  • Add similar structures for other services (Functions, ML Kit, ...)
  • Use hooks where sensible
  • Change build structure to be able to serve different services individually allowing tree-shaking unused services away
    import { FirebaseProvider } from 'react-firebase-context';
    import { FirestoreProvider, Firestore } from 'react-firebase-context/firestore';
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].