Giter Club home page Giter Club logo

fireview's Introduction

Fireview

A quick helper to map data in Firebase to React views. Supports Cloud Firestore.

Requires React 16, since the <Map> component renders an array.

Example

This very repo is an example of how to use Fireview:

In an ideal world, our views wouldn't care about what DB they're rendering. And, indeed, the views are almost identical. A future version of Fireview will likely change the API to allow them to be entirely identical.

Here's our message component:

  // Message.jsx
  // from and body are fields in the database.
  // _ref is the reference we're rendering. We can use this
  // to mutate the database.
  const Message = ({from, body, _ref}) =>
    <li>
      <strong>{from}:</strong> {body}
      <a onClick={() => _ref.set(null)}>⛔️</a>  { /* Delete this message */ }
    </li>

We're going to map a collection of messages to it.

Whether you're using Firestore or the legacy Realtime database, <Map> listens to the reference you provide and updates the view when the database changes.

With Cloud Firestore

  // Messages-Firestore.jsx
  import {Map} from 'fireview'

  export default () => 
    <ul>
      <Map from={
          // In a real app, you'd probably take in this reference
          // as a prop.

          // from accepts any Firestore query.
          firebase.firestore()
            .collection('messages')
        }
        // Loading takes a component that is displayed while
        // the first snapshot is loading.
        Loading={() => 'Loading...'}

        // Render takes a component that renders each document in
        // the returned query
        Render={Message}

        // Empty takes a component displayed if the collection is empty.
        Empty={() => 'No messages here.'}
      />
    </ul>

<Map> renders your Render component once for each Document in the query you provide it (so if your query references a single Document, you only get that one.)

With the Realtime Database

Using the Realtime DB is very similar. One difference is that you need to give <Map> an each prop if you want your Render component to be mounted once per each child (rather than once for the entire path).

(This is because the Realtime Database doesn't distinguish between "Documents" and regular values.)

  // Messages-Realtime.jsx 
  import {Map} from 'fireview'

  export default () =>
    <ul>      
      <Map each from={firebase.database().ref('/chatrooms/welcome')}
        // ⬆️ "each" means we'll map over all children of this path
        // Everything else behaves the same.
        Loading={() => 'Loading...'}
        Render={Message}
        Empty={() => 'No messages here.'}
      />
    </ul>

Using AuthProvider

You tend to need information about the currently logged in user in various places around your app.

<AuthProvider> provides auth information.

withAuth is a HOC that takes this auth information and adds a withAuth prop to the component it wraps. This prop has the shape:

{
  // The current user
  user: firebase.auth.User,
  
  // The firebase Auth interface
  auth: firebase.auth.Auth,

  // True once the auth state has resolved (once
  // onAuthStateChanged has emitted at least once).
  ready: bool,
}

(Note: the HOC also adds _user, _auth, and _authReady props, but these are uglier and will be removed in the future. Use the nested object instead.)

import * as firebase from 'firebase'

import {AuthProvider, withAuth} from 'fireview'

export default () =>
  <AuthProvider auth={firebase.auth()}>
    {/* ShowUid is a direct child here, but it doesn't have to be. */}
    <ShowUid />
  </AuthProvider>

const ShowUid = withAuth(
  ({withAuth: {user, auth}}) =>
    user
      ? user.uid
      : <a onClick={() => auth.signInAnonymously()}>
          Sign in
        </a>
)

fireview's People

Contributors

queerviolet avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.