|
| 1 | +# Firebase-react-store |
| 2 | + |
| 3 | +Easily use Firebase like a store. |
| 4 | + |
| 5 | +Often project that use Firebase will map data from the real-time |
| 6 | +database into their own project store (or component) manually using |
| 7 | +several React lifecycle methods. Sometimes causing a two-way data |
| 8 | +binding problem when processing updates to the local store and |
| 9 | +reliably keeping both in sync. |
| 10 | + |
| 11 | +Instead, FRS makes it possible to use Firebase through props just like |
| 12 | +a store, without additional mapping or configuration. |
| 13 | + |
| 14 | +## Quick introduction |
| 15 | +### Connecting |
| 16 | +```js |
| 17 | +import {RTDatabase} from 'firebase-react-store'; |
| 18 | + |
| 19 | +// connect to Firebase |
| 20 | +export const rtdb = new RTDatabase(firebase_config); |
| 21 | +```` |
| 22 | + |
| 23 | +This example will connect to Firebase by creating a new instance of |
| 24 | +`RTDatabase`. This is a reusable connection that should be shared |
| 25 | +between components. |
| 26 | + |
| 27 | +### Observing Documents |
| 28 | + |
| 29 | +Documents describe a path in Firebase. They are created using `RTDatabase.get`: |
| 30 | + |
| 31 | +```js |
| 32 | +const group = rtdb.get('/some/group'); |
| 33 | +``` |
| 34 | + |
| 35 | +FRS also provides an `observer` decorator to easily react to changes |
| 36 | +on a document. |
| 37 | + |
| 38 | +```js |
| 39 | +@observer |
| 40 | +class View extends PureComponent { |
| 41 | + render() { |
| 42 | + return <p>{group.value.name}</p>; |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +Accesses to documents by the render |
| 48 | +function above will be tracked. When changes are made in the database, |
| 49 | +the View component will re-render. This is a one-way mapping, which |
| 50 | +keeps with the design philosophy of React. Also, there's no need to |
| 51 | +use lifecycle methods in this example. |
| 52 | + |
| 53 | +Documents can also be used to update data on Firebase through `set` and `push`: |
| 54 | + |
| 55 | +```js |
| 56 | +// set properties on this document |
| 57 | +group.set({name: 'Updated Name'}); |
| 58 | + |
| 59 | +// or push new child nodes with a server-side key |
| 60 | +group.push({name: 'New group'}) |
| 61 | +``` |
| 62 | + |
| 63 | +### Collections |
| 64 | + |
| 65 | +```js |
| 66 | +import {collectionObserver} from 'firebase-react-store'; |
| 67 | + |
| 68 | +@collectionObserver({database: rtdb, path: '/some/collection'}) |
| 69 | +class MessageCollection extends PureComponent { |
| 70 | + render() { |
| 71 | + return this.props.collection.map((v) => <Message message={v.value} />); |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +The `collectionObserver` decorator will create a query and list the |
| 77 | +nodes found at the path `/some/collection`. When `collectionObserver` |
| 78 | +is notified of a child being added or removed from the path, it will |
| 79 | +cause `MessageCollection` to rerender. |
0 commit comments