Skip to content

Commit 06a8ef9

Browse files
abeisgoatJacob Wenger
authored andcommitted
Upgraded to the Firebase 3.x.x SDK (#91)
1 parent a0071f1 commit 06a8ef9

22 files changed

+733
-151
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.DS_Store
22
npm-debug.log
33

4+
tests/key.json
5+
46
dist/
57
coverage/
68
node_modules/

.travis.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ node_js:
44
- '0.12'
55
# - stable
66
sudo: false
7+
env:
8+
REACTFIRE_TEST_DB_URL=https://reactfire-bbede.firebaseio.com
79
install:
810
- npm install
911
before_script:
1012
- export DISPLAY=:99.0
1113
- sh -e /etc/init.d/xvfb start
1214
script:
13-
- npm run travis
15+
- "[ -e tests/key.json ] && npm run travis || false"
1416
after_script:
1517
- cat ./tests/coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
18+
before_install:
19+
- openssl aes-256-cbc -K $encrypted_f7671fde2e57_key -iv $encrypted_f7671fde2e57_iv
20+
-in tests/key.json.enc -out tests/key.json -d

README.md

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1-
# ReactFire
1+
# ReactFire [![Build Status](https://travis-ci.org/firebase/reactfire.svg?branch=master)](https://travis-ci.org/firebase/reactfire) [![Coverage Status](https://coveralls.io/repos/firebase/reactfire/badge.svg?branch=master&service=github)](https://coveralls.io/github/firebase/reactfire?branch=master) [![GitHub version](https://badge.fury.io/gh/firebase%2Freactfire.svg)](http://badge.fury.io/gh/firebase%2Freactfire)
22

3-
[![Build Status](https://travis-ci.org/firebase/reactfire.svg?branch=master)](https://travis-ci.org/firebase/reactfire)
4-
[![Coverage Status](https://coveralls.io/repos/firebase/reactfire/badge.svg?branch=master&service=github)](https://coveralls.io/github/firebase/reactfire?branch=master)
5-
[![GitHub version](https://badge.fury.io/gh/firebase%2Freactfire.svg)](http://badge.fury.io/gh/firebase%2Freactfire)
63

7-
[ReactJS](http://facebook.github.io/react/) is a framework for building large, complex user
8-
interfaces. [Firebase](http://www.firebase.com/?utm_source=reactfire) complements it perfectly
9-
by providing an easy-to-use, realtime data source for populating the `state` of React components.
10-
With [ReactFire](https://www.firebase.com/docs/web/libraries/react/?utm_source=reactfire), it only
11-
takes a few lines of JavaScript to integrate Firebase data into React apps via the `ReactFireMixin`.
4+
[ReactJS](https://facebook.github.io/react/) is a framework for building large, complex user
5+
interfaces. [Firebase](https://firebase.google.com/) complements it perfectly by providing an
6+
easy-to-use, realtime data source for populating the `state` of React components. With ReactFire, it
7+
only takes a few lines of JavaScript to integrate Firebase data into React apps via the
8+
`ReactFireMixin`.
129

13-
[Read through our documentation](https://www.firebase.com/docs/web/libraries/react/?utm_source=reactfire)
14-
on using Firebase with React and [check out our live Todo app demo](https://reactfiretodoapp.firebaseapp.com/)
15-
to get started!
10+
11+
## Table of Contents
12+
13+
* [Getting Started With Firebase](#getting-started-with-firebase)
14+
* [Downloading ReactFire](#downloading-reactfire)
15+
* [Documentation](#documentation)
16+
* [Examples](#examples)
17+
* [Migration Guides](#migration-guides)
18+
* [Contributing](#contributing)
19+
20+
21+
## Getting Started With Firebase
22+
23+
ReactFire requires [Firebase](https://firebase.google.com/) in order to sync and store data.
24+
Firebase is a suite of integrated products designed to help you develop your app, grow your user
25+
base, and earn money. You can [sign up here for a free account](https://console.firebase.google.com/).
1626

1727

1828
## Downloading ReactFire
@@ -25,62 +35,42 @@ In order to use ReactFire in your project, you need to include the following fil
2535
<script src="https://fb.me/react-dom-15.0.1.min.js"></script>
2636

2737
<!-- Firebase -->
28-
<script src="https://cdn.firebase.com/js/client/2.4.0/firebase.js"></script>
38+
<script src="https://www.gstatic.com/firebasejs/3.0.3/firebase.js"></script>
2939

3040
<!-- ReactFire -->
31-
<script src="https://cdn.firebase.com/libs/reactfire/0.7.0/reactfire.min.js"></script>
41+
<script src="https://cdn.firebase.com/libs/reactfire/1.0.0/reactfire.min.js"></script>
3242
```
3343

34-
Use the URL above to download both the minified and non-minified versions of ReactFire from the
35-
Firebase CDN. You can also download them from the
36-
[releases page of this GitHub repository](https://github.com/firebase/reactfire/releases).
37-
[Firebase](https://www.firebase.com/docs/web/quickstart.html?utm_source=reactfire) and
38-
[React](http://facebook.github.io/react/downloads.html) can be downloaded directly from their
39-
respective websites.
40-
4144
You can also install ReactFire via npm or Bower. If downloading via npm, you will have to install
4245
React and Firebase separately (that is, they are `peerDependencies`):
4346

4447
```bash
4548
$ npm install reactfire react firebase --save
4649
```
4750

48-
On Bower, the React and Firebase dependencies will be downloaded automatically:
51+
On Bower, the React and Firebase dependencies will be downloaded automatically alongside ReactFire:
4952

5053

5154
```bash
5255
$ bower install reactfire --save
5356
```
5457

58+
## Documentation
5559

56-
## Getting Started with Firebase
57-
58-
ReactFire requires Firebase in order to store data. You can
59-
[sign up here for a free account](https://www.firebase.com/signup/?utm_source=reactfire).
60-
60+
* [Quickstart](docs/quickstart.md)
61+
* [Guide](docs/guide.md)
62+
* [API Reference](docs/reference.md)
6163

62-
## Usage
6364

64-
To use the `ReactFireMixin` in a React component, add it to the component's `mixins` property:
65-
66-
```javascript
67-
var ExampleComponent = React.createClass({
68-
mixins: [ReactFireMixin],
69-
...
70-
});
71-
```
65+
## Examples
7266

73-
The ReactFire APIs will then be available from the `this` object inside of `ExampleComponent`.
67+
* [Todo App](examples/todoApp)
68+
* [Comments Box](examples/commentsBox)
7469

7570

76-
## Documentation and API Reference
71+
## Migration Guides
7772

78-
The [ReactFire quickstart](https://www.firebase.com/docs/web/libraries/react/quickstart.html?utm_source=reactfire)
79-
is a great place to get started. There is a walkthrough on how to create the
80-
[Todo app demo](https://reactfiretodoapp.firebaseapp.com/) in the
81-
[ReactFire guide](https://www.firebase.com/docs/web/libraries/react/guide.html?utm_source=reactfire).
82-
Finally, there is a [full API reference](https://www.firebase.com/docs/web/libraries/react/api.html?utm_source=reactfire)
83-
as well.
73+
* [Migrating from ReactFire `0.7.0` to `1.x.x`](docs/migration/070-to-1XX.md)
8474

8575

8676
## Contributing

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
],
3434
"dependencies": {
3535
"react": "0.13.x || 0.14.x || 15.x.x",
36-
"firebase": "2.x.x"
36+
"firebase": "2.x.x || 3.x.x"
3737
}
3838
}

docs/guide.md

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
# Guide | ReactFire
2+
3+
4+
## Table of Contents
5+
6+
* [What Is ReactJS?](#what-is-reactjs)
7+
* [Adding Firebase to Your React App](#adding-firebase-to-your-react-app)
8+
* [`ReactFireMixin`](#reactfiremixin)
9+
* [Next Steps](#next-steps)
10+
11+
12+
## What Is ReactJS?
13+
14+
[ReactJS](http://facebook.github.io/react/) is a JavaScript library built by Facebook and Instagram
15+
which makes it easy to build large, complex user interfaces. The creators of React describe it as
16+
the “V[iew] in MVC.” It is not meant to be a replacement for Angular or Ember; instead, it is meant
17+
to extend their functionality by providing a high-performance way to keep a view up-to-date with
18+
JavaScript. Its special sauce is that it renders HTML using an incredibly fast virtual DOM diff
19+
algorithm, providing much better performance than competing platforms. It has a “one-way reactive
20+
data flow” which is much simpler to understand than traditional data-binding.
21+
22+
Components - the basic building blocks of React apps - are organized in a tree hierarchy in which
23+
parent components send data down to their children through the props variable. Each component also
24+
has a `state` variable which determines the current data for its view. Whenever `state` is changed,
25+
the component’s render() method is called and React figures out the most efficient way to update the
26+
DOM.
27+
28+
Since React’s main focus is on the user interface, React apps need something else to act as their
29+
backend. That is where Firebase comes in. It adds the “M[odel] and C[ontroller] in MVC” to React
30+
apps, making them fully functioning apps. **Using React’s straightforward binding system, it is easy
31+
to integrate Firebase in a native way with only a small amount of code.**
32+
33+
34+
## Adding Firebase to Your React App
35+
36+
Let's look at the Todo app on the [React homepage](http://facebook.github.io/react/). Within the
37+
`TodoApp` component, `this.state` is used to keep track of the input text and the list of Todo
38+
items. While React ensures that the DOM stays in sync with any changes to `this.state`, the changes
39+
do not persist beyond the life of the page. If you add a few items and then refresh the page, all of
40+
the items disappear! This is because **React has no mechanism for storing data beyond the scope of
41+
the current page session**. It relies on being used with another framework to do that.
42+
43+
**Firebase is a natural complement to React as it provides React apps with a persistent, realtime
44+
backend**. The first thing we need to do is add Firebase to your project:
45+
46+
```js
47+
<!-- React JS -->
48+
<script src="https://fb.me/react-15.1.0.js"></script>
49+
<script src="https://fb.me/react-dom-15.1.0.js"></script>
50+
51+
<!-- Firebase -->
52+
<script src="https://www.gstatic.com/firebasejs/3.0.3/firebase.js"></script>
53+
```
54+
55+
We'll need to initialize the Firebase SDK before we can use it. This should happen one time, outside
56+
of your React component. You can find more details on the [web](https://firebase.google.com/docs/web/setup)
57+
or [Node.js](https://firebase.google.com/docs/server/setup) setup guides.
58+
59+
```js
60+
<script>
61+
var config = {
62+
apiKey: "<YOUR-API-KEY>",
63+
64+
// Only needed if using Firebase Realtime Database (which we will be in this example)
65+
databaseURL: "https://<YOUR-DATABASE-NAME>.firebaseio.com",
66+
67+
// Only needed if using Firebase Authentication
68+
authDomain: "<YOUR-AUTH-DOMAIN>",
69+
70+
// Only needed if using Firebase Storage
71+
storageBucket: "<YOUR-STORAGE-BUCKET>.appspot.com"
72+
};
73+
74+
firebase.initializeApp(config);
75+
</script>
76+
```
77+
78+
Now that we have included Firebase, we can populate the list of Todo items by reading them from the
79+
database. We do this by hooking into the `componentWillMount()` method of the `TodoApp` component
80+
which is run once, immediately before the initial rendering of the component:
81+
82+
```js
83+
componentWillMount: function() {
84+
this.firebaseRef = firebase.database.ref("items");
85+
this.firebaseRef.on("child_added", function(dataSnapshot) {
86+
this.items.push(dataSnapshot.val());
87+
this.setState({
88+
items: this.items
89+
});
90+
}.bind(this));
91+
}
92+
```
93+
94+
This code first gets a reference to the `items` node at the root of the database. The call to `on()`
95+
will be run every time a node is added under the `items` node. It is important to realize that a
96+
`child_added` event will be fired for every item under the `items` node, not just new ones that are
97+
added to it. Therefore, when the page is loaded, every existing child under the `items` node will
98+
fire a `child_added` event, meaning they can easily be iterated over and added to `this.state.items`.
99+
Note that the call at the end to `bind()` just sets the scope of callback function to this.
100+
101+
What about adding new Todo items to the database? That code is just as easy:
102+
103+
```js
104+
handleSubmit: function(e) {
105+
e.preventDefault();
106+
this.firebaseRef.push({
107+
text: this.state.text
108+
});
109+
this.setState({text: ""});
110+
}
111+
```
112+
113+
Within `handleSubmit()` a new item is pushed onto the database reference which appends it to the end
114+
of the `items` node. The call to `setState()` updates `this.state.text` but does not need to update
115+
`this.state.items` as it did in the original React code. This is because the `child_added` event
116+
handler from `componentWillMount()` will be fired when a new child is pushed onto the `items` node
117+
and that code will update `this.state.items`.
118+
119+
The last thing that needs to happen is cleaning up the database event handler:
120+
121+
```js
122+
componentWillUnmount: function() {
123+
this.firebaseRef.off();
124+
}
125+
```
126+
127+
With just the few changes above, items added to the Todo list are updated in realtime. Best of all,
128+
the items stick around if the page is refreshed! You can even open multiple tabs pointed at the same
129+
page and see them all update simultaneously, with Firebase doing all the heavy lifting. Take some
130+
time to [view the code for this example](https://github.com/firebase/ReactFire/blob/master/examples/todoApp/js/todoAppFirebaseExplicit.js)
131+
and [play around with a live demo](https://reactfiretodoapp.firebaseapp.com/).
132+
133+
134+
## `ReactFireMixin`
135+
136+
Although integrating Firebase into a React app only takes a few lines of code out of the box, we
137+
wanted to make it even easier. We also want to be able to handle when array items are removed or
138+
updated from Firebase. **We built the `ReactFireMixin` to make it simple to keep `this.state` in
139+
sync with a database node.**
140+
141+
To get started with ReactFire, include it in your project by loading the library directly from our
142+
CDN and placing it right after the React and Firebase libraries in the `<head>` tag:
143+
144+
```js
145+
<!-- React JS -->
146+
<script src="https://fb.me/react-15.1.0.js"></script>
147+
<script src="https://fb.me/react-dom-15.1.0.js"></script>
148+
149+
<!-- Firebase -->
150+
<script src="https://www.gstatic.com/firebasejs/3.0.3/firebase.js"></script>
151+
152+
<!-- ReactFire -->
153+
<script src="https://cdn.firebase.com/libs/reactfire/1.0.0/reactfire.min.js"></script>
154+
```
155+
156+
ReactFire and its dependencies are also available from npm via `npm install reactfire` and Bower
157+
via `bower install reactfire`.
158+
159+
After making sure to initialize the Firebase SDK again, we can then use the `ReactFireMixin` in the
160+
`TodoApp` component, add it to the component's `mixins` property:
161+
162+
```js
163+
var TodoApp = React.createClass({
164+
mixins: [ReactFireMixin],
165+
...
166+
});
167+
```
168+
169+
The `ReactFireMixin` extends the functionality of the `TodoApp` component, adding additional
170+
Firebase-specific methods to it. To keep `this.state.items` in sync with any changes to the `items`
171+
node, make the following change in `componentWillMount()`:
172+
173+
```js
174+
componentWillMount: function() {
175+
var ref = firebase.database().ref("items");
176+
this.bindAsArray(ref, "items");
177+
}
178+
```
179+
180+
We simply specify that we want to bind a particular Firebase Database reference to `this.state.items`
181+
of the React component. The `ReactFireMixin` allows binding to a node as an array or as a regular
182+
JavaScript object. This creates a one-way binding from the `Firebase` reference to `this.state.items`,
183+
meaning that if the data in the database changes, so will `this.state.items`. However, if we update
184+
`this.state.items`, the database data will not change. Therefore, changes should be made directly to
185+
the database and not by calling `setState()`:
186+
187+
```js
188+
handleSubmit: function(e) {
189+
e.preventDefault();
190+
this.firebaseRefs.items.push({
191+
text: this.state.text
192+
});
193+
this.setState({ text: "" });
194+
}
195+
```
196+
197+
**ReactFire allows for binding to multiple things at once.** Firebase ensures that this is all done
198+
in an efficient manner. To access the `Firebase` reference which is bound to `this.state.items`, we
199+
can reference `this.firebaseRefs["items"]` which is provided by ReactFire. Finally, calling
200+
`this.firebaseRef.off()` is no longer needed in `componentWillUnmount()` as the mixin handles this
201+
behind the scenes.
202+
203+
You can [view the code for this example](https://github.com/firebase/ReactFire/blob/master/examples/todoApp/js/todoAppFirebaseImplicit.js)
204+
and [play around with a live demo](https://reactfiretodoapp.firebaseapp.com/). The code and demo add
205+
the ability to delete items in the array and have them automatically synced back to `this.state.items`.
206+
207+
208+
## Next Steps
209+
210+
ReactJS is a wonderful framework for creating user interfaces. When picking a complementary tool to
211+
use alongside it as a backend, Firebase is the easiest and most powerful solution. In just a few
212+
lines of code you can get a React app syncing data across hundreds of clients at once. ReactFire
213+
makes this that much easier, getting rid of even more boilerplate code.
214+
215+
Head on over to the [ReactFire API reference](reference.md) and then get started building an app
216+
with ReactFire!

docs/migration/070-to-1XX.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Migrating from ReactFire `0.7.0` to `1.x.x`
2+
3+
This migration document covers all the major breaking changes mentioned in the [ReactFire `1.0.0`
4+
change log](https://github.com/firebase/reactfire/releases/tag/v1.0.0).
5+
6+
7+
## Upgrade your Firebase SDK
8+
9+
While ReactFire `1.0.0` continues to support all `2.x.x` versions of the Firebase SDK, we encourage
10+
you to upgrade to version `3.x.x`. See the [Firebase `3.x.x` migration
11+
guide](https://firebase.google.com/support/guides/firebase-web) for more details.

0 commit comments

Comments
 (0)