Firebase + Redux for React
Firedux (fiery·ducks) wraps the Firebase JavaScript API to dispatch Redux actions that optimisically & immediately read/write to an in-memory subset of your data from Firebase, then asynchronously pull & push data in the background.
Also supports some authentication methods and actions.
Works well with React.
Support firedux and fiery ducks like Magmar!
Install
Install with npm
You'll need to configure redux-thunk
on your Redux store.
The browser build (dist/src/index.browser.js
) should support UMD (AMD, CommonJS, and globals). If using globals with Firebase 2.x you may need to alias Firebase
as firebase
prior to loading this module. Other shims may be necessary. Please report any issues or findings.
Dependencies:
firebase
lodash
: as_
if using globalsupdeep
Promise
: polyfill globally as needed
Use
See my TodoMVC example, the tests, and below:
/** * For Firebase 2.x, e.g.: */var ref = 'https://redux-firebase.firebaseio.com/' /** * Or for Firebase 3.x:, e.g.: */var app = Firebasevar ref = app /** * Then create your Firedux instance, passing it your database reference as `ref`. */const firedux = ref // Optional: omit: '$localState' // Properties to reserve for local use and not sync with Firebase. const reducer = // Create store with middleware, including thunk. const middleware = const store = // Set dispatch function from store on your Firedux instance.fireduxdispatch = storedispatch // Later, you can subscribe to state.store // Watch a path:firedux// state.firedux.data.users.joe// Note: this promise will only resolve on the first value,// but it'll keep syncing on all value updates. // Get:firedux// state.firedux.data.posts['123'] // Set:firedux// state.firedux.data.test == true // Update (merging set):firedux// state.firedux.data.users.joe == { name: 'Joe', job: 'developer' } // Push (to a collection):firedux// state.firedux.data.users['-K95Cjx-caw2uSNsFJiI'] == { name: 'Jane' } // Remove:firedux// state.firedux.data.users['joe'] == undefined // Auth // Init// Call this when your app starts, to get existing session, and listen for auth changes.firedux// See Login state below. // Loginfiredux// state.firedux.authData == { auth: { uid: '123' } }// etc. `authData` per https://www.firebase.com/docs/web/api/firebase/authwithcustomtoken.html// or state.firedux.authError == Error // Logoutfiredux// state.firedux.authData == null // To handle some unsupported features, you can get access to the underlying Firebase instance via:fireduxref// e.g. turn off a watch:fireduxref// or login with OAuth:fireduxref
Analytics
This component includes tracking via Google Analytics. The purpose is to better understand how and where it's used, as a guide for development.
To opt-out of this tracking, before loading the script on your page,
use the global options in JavaScript, with noTrack
set to true
, as follows:
windowFIREDUX_OPTIONS = noTrack: true;
Running tests
Install dev dependencies:
$ npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
In lieu of a formal styleguide, please:
- Take care to maintain the existing coding style
- Add unit tests for any new or changed functionality
- Re-build documentation with verb-cli before submitting a pull request.
Author
Anders D. Johnson
License
Copyright © 2015-2016 Anders D. Johnson Released under the MIT license.
This file was generated by verb-cli on August 6, 2016.