0.1.5 • Published 5 years ago

fireact v0.1.5

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

Fireact

Build Status

Explainer

Fireact is a library of React hooks that provide easy access to Firebase products inside your React app.

Example use case

With hooks like useFirebaseDatabaseState, there's an easy API for:

  • subscribing a component to data in your Firebase Real-Time Database;
  • triggering updates to your data in your Firebase Real-Time Database.

Installation

npm install --s fireact

Main API

import Fireact from 'fireact'

// Retrieve your own options values by adding a web app on
// https://console.firebase.google.com
const config = {
  apiKey: "AIza....",                             // Auth / General Use
  authDomain: "YOUR_APP.firebaseapp.com",         // Auth with popup/redirect
  databaseURL: "https://YOUR_APP.firebaseio.com", // Realtime Database
  storageBucket: "YOUR_APP.appspot.com",          // Storage
  messagingSenderId: "123456789"                  // Cloud Messaging
}

const products = [
  'auth',
  'database'
  // ...include any other Firebase products you want to use
]

const {
  firebase,     // firebase object with API as documented: https://firebase.google.com/docs/reference/js/
  Provider,     // Parent Provider that allows Fireact hooks to be used in components nested within
  middleware    // Redux middleware that makes the firebase object available as a property of all actions
} = Fireact(config, products)

Any components that are wrapped in Provider, or have a parent/ancestor wrapped in Provider, gain access to the library's hooks.

Most notable of these are:

Hooks

Hooks can be used inside any component that has Provider wrapped around it.

useFirebase()

Returns

The firebase object initialised by Fireact.

Example

import React from 'react'
import { useFirebase } from 'fireact'

function Component() {
  const firebase = useFirebase()

  // exposes the JS Firebase API
  // docs at https://firebase.google.com/docs/reference/js/
}

useFirebaseCurrentUser()

Returns

The current user from Firebase Authentication, if there is one.

Example

import React from 'react'
import { useFirebaseCurrentUser } from 'fireact'

function Component() {
  const user = useFirebaseCurrentUser()

  // exposes the firebase.User object for the current Firebase user
  // docs at https://firebase.google.com/docs/reference/js/firebase.User.html
}

useFirebaseDatabaseState(path, [options = {}])

Parameters

  • path (string): path to a value in the Firebase Real-Time Database
  • options (object, optional): a configuration object for sorting and filtering

Returns

An array with two elements: 1. The current value of the Firebase Real-Time Database at path; and 2. An object of functions which can be used to write to Firebase Real-Time Database at path.

These are the two return values, respectively, from useFirebaseDatabaseValue and useFirebaseDatabaseWriters.

Example

import React from 'react'
import { useFirebaseDatabaseState } from 'fireact'

function Component() {
  const [value, { set, transaction, update, push, pushWithKey }] = useFirebaseDatabaseState('arbitrary/path/to/entry')

  // your logic here
}

useFirebaseDatabaseValue(path, [options = {}])

Parameters

  • path (string): path to a value in the Firebase Real-Time Database
  • options (object, optional): a configuration object for sorting and filtering

Returns

The current value of the Firebase Real-Time Database at path.

Example

import React from 'react'
import { useFirebaseDatabaseValue } from 'fireact'

function Component() {
  const value = useFirebaseDatabaseValue('arbitrary/path/to/entry')

  // exposes the JS value from the Firebase RTD database location of path
}

options object: sorting and filtering

KeyValue (type)DescriptionFirebase Docs
orderByChildstringUses the given value as a child key to order the datafirebase.database.Reference.orderByChild
orderByKeybooleanIf true, orders the data by keyfirebase.database.Reference.orderByKey
orderByPrioritybooleanIf true, orders the data by priorityfirebase.database.Reference.orderByPriority
orderByValuebooleanIf true, orders the data by valuefirebase.database.Reference.orderByValue
limitToFirstnumberRetrieves only the first limitToFirst number of childrenfirebase.database.Reference.limitToFirst
limitToLastnumberRetrieves only the last limitToLast number of childrenfirebase.database.Reference.limitToLast
startAtnumber, string or booleanfirebase.database.Reference.startAt
endAtnumber, string or booleanfirebase.database.Reference.endAt
equalTonumber, string or booleanfirebase.database.Reference.equalTo

useFirebaseDatabaseWriters(path)

Parameters

  • path (string): path to a value in the Firebase Real-Time Database

Returns

An object of functions which can be used to write to Firebase Real-Time Database at path:

FunctionDescriptionFirebase Docs
setTakes a value and updates the RTD to the given value at pathfirebase.database.Reference.set
transactionTakes a callback and updates the RTD with the return value from the callback when it is passed the RTD's current value at pathfirebase.database.Reference.transaction
updateTakes an object and updates the RTD by assigning the object's key-value pairs at pathfirebase.database.Reference.update
pushTakes an value, auto-generates a push key for it, and updates the RTD at said key from path with the passed in valuefirebase.database.Reference.push
pushWithKeyTakes a callback, (autoGeneratedPushKey) => value, which updates the RTD at the auto-generated push key from path (as above, with the vanilla push) to the return value of the callbackN/A

Example

import React from 'react'
import { useFirebaseDatabaseWriters } from 'fireact'

function Component() {
  const {
    set,
    transaction,
    update,
    push,
    pushWithKey
  } = useFirebaseDatabaseWriters('arbitrary/path/to/entry')

  // functions can be executed inside a useEffect hook, component callback, etc.
}
0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago