You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44-42Lines changed: 44 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
This project was bootstrapped with [nwb](https://github.com/insin/nwb)
4
4
5
-
Firekit was created to help working with Firebase in React Projects that use Redux as state storage.
5
+
Firekit was created to help working with Firebase in React Projects that use Redux as state storage.
6
6
7
7
You can find a full functional **DEMO** project (React Most Wanted) with source code [here](https://www.react-most-wanted.com/).
8
8
@@ -15,7 +15,7 @@ You can find a full functional **DEMO** project (React Most Wanted) with source
15
15
-[Usage](#usage)
16
16
-[Accessing firebaseApp](#accessing-firebaseapp)
17
17
-[Connection](#connection)
18
-
-[Lists](#lists)
18
+
-[Lists and Queries](#lists-and-queires)
19
19
-[Paths](#paths)
20
20
-[FireForm](#fireform)
21
21
-[TO DO](#to-do)
@@ -26,7 +26,7 @@ You can find a full functional **DEMO** project (React Most Wanted) with source
26
26
Firekit allows you to watch firebase data and sync it to your redux store with a minimum of code to write. It uses a `Provider` to server the `firebaseApp` to all Components that need it.
27
27
28
28
Some features that are unque to this firebase toolkit are:
29
-
***persistant watchers** - the watchers are persistant and are not linked to components. You deside when to watch a value in your firebase database and when to unwatch (turn off listeners) it.
29
+
***persistant watchers** - the watchers are persistant and are not linked to components. You deside when to watch a value in your firebase database and when to unwatch (turn off listeners) it.
30
30
31
31
***your create firebaseApp** - you initialise the firebaseApp how you want and add it as prop to the firekit `FirebaseProvider` and all your components have access to the firebaseApp
32
32
@@ -36,7 +36,7 @@ Some features that are unque to this firebase toolkit are:
36
36
37
37
***native firebase** - you can use firebases native sdk for the web. Firekit is just listening on changes. Every change on the data you can make as it is described in the official firebase documentation
38
38
39
-
***realtime forms** - firekit has a special Warapper for `redux-forms` witch allows to sync them with the realtime database very simple and plus it is automaticaly synced on field changes in real time if they ocure while your are in the Form
39
+
***realtime forms** - firekit has a special Warapper for `redux-forms` witch allows to sync them with the realtime database very simple and plus it is automaticaly synced on field changes in real time if they ocure while your are in the Form
40
40
41
41
Features like populating values in the database are omited with purpose. The firebase `cloud functions` are the place where you should populate data that must be saved in multiple places.
42
42
@@ -110,46 +110,41 @@ import dialogs from './dialogs/reducer';
110
110
importmessagingfrom'./messaging/reducer';
111
111
importlocalefrom'./locale/reducer';
112
112
importthemefrom'./theme/reducer';
113
-
import {
114
-
connectionReducer,
115
-
listsReducer,
116
-
pathsReducer,
117
-
initializationReducer
118
-
} from'firekit'; //Import the reducers
113
+
importfirekitReducersfrom'firekit'; //Import the firekitReducers
114
+
115
+
116
+
console.log(firekitReducers);
119
117
120
118
constreducers=combineReducers({
121
119
browser: responsiveStateReducer,
122
120
responsiveDrawer,
123
121
form: formReducer,
124
122
auth,
125
-
initialization: initializationReducer, //Add the initialisation reducer
126
-
connection: connectionReducer, //Add the connection reducer
127
-
lists: listsReducer, //Add the lists reducer
128
-
paths: pathsReducer, //Add the paths reducer
129
123
dialogs,
130
124
messaging,
131
125
locale,
132
126
theme,
127
+
...firekitReducers //Spread the firekit reducers
133
128
})
134
129
135
130
exportdefaultreducers;
136
131
137
132
```
138
-
In future versions the reducers will be combined in a single object that will be reducable.
133
+
To add all firekti reducers to your redux store just spread the firekitReducers object into your `comineReducers` object.
139
134
140
135
**WARNING:** if you are using persistance take care that the reducer `initialization` is not persisted! He saves the watchers. If he would be persisted the watcher would not initialize again after a page reload. If you are using `redux-persist` just add him to the black list.
141
136
142
137
```js
143
138
persistStore(store, {blacklist:['auth', 'form', 'connection', 'initialization'] }, ()=>{}); //Add initialization to persistance blacklist if you use persistance
144
139
```
145
140
146
-
**INFO:** the reducers are not customasable so they have to use the names as they are here in the snipped. In future we could add customatisation for this so they could have any name you want.
141
+
**INFO:** the reducers are not customasable. In future we could add customatisation for this so they could have any name you want.
147
142
148
143
## Usage
149
144
150
145
Let us now do something with our firekit :smile:
151
-
To use `firekit` in a component we need to tell the component to get all `firekit` props from the context.
152
-
We use for that a simple call `withFirebase`. It is very similar to the `react-router` call `withRouter`. The usage is exactly the same.
146
+
To use `firekit` in a component we need to tell the component to get all `firekit` props from the context.
147
+
We use for that a simple call `withFirebase`. It is very similar to the `react-router` call `withRouter`. The usage is exactly the same.
153
148
154
149
Let us take a look on a simple component.
155
150
@@ -247,7 +242,7 @@ class MyComponent extends Component {
247
242
const { initConnection }=this.props;
248
243
initConnection(); //Here we started watching the connection state
249
244
}
250
-
245
+
251
246
componentWillUnmount() {
252
247
const { unsubscribeConnection, }=this.props;
253
248
unsubscribeConnection(); // Here we unsunscribe the listening to the connection state
@@ -276,9 +271,9 @@ export default connect(
276
271
)(withFirebase(MyComponent));
277
272
```
278
273
279
-
### Lists
274
+
### Lists and Queries
280
275
281
-
We can easely observe lists in the realtime database using the `watchList` and `unwatchList` API calls.
276
+
We can easely observe lists in the realtime database using the `watchList` and `unwatchList` API calls. The same calls are used to observe Firebase queries. `watchList` and `unwatchList` can recieve as parameter a string to a database path or a Firebase reference to that path. If you have a simple reference to a path using just the string of the path is the right choice. But if you have a Firebase query reference you can send that reference with all its query calls as parameter.
282
277
283
278
```js
284
279
importReact, { Component } from'react';
@@ -289,18 +284,25 @@ import _ from 'lodash';
289
284
classMyComponentextendsComponent {
290
285
291
286
componentDidMount(){
292
-
const { watchList }=this.props;
293
-
watchList('users'); //Here we started watching the list
287
+
const { watchList, firebaseApp }=this.props;
288
+
289
+
watchList('users'); //Here we started watching the users list
290
+
291
+
//Her we watch a simple firebase query
292
+
let tasksRef=firebaseApp.database().ref('tasks').limitToFirst(10);
293
+
watchList(tasksRef);
294
+
294
295
}
295
-
296
+
296
297
componentWillUnmount() {
297
298
const { unwatchList, }=this.props;
298
299
unwatchList('users'); // We can unwatch the list on unmounting the Component
300
+
unwatchList('tasks'); // To unwatch a query qe can use just the ref path string
299
301
}
300
-
302
+
301
303
rednerList= () => {
302
304
const {users} =this.props;
303
-
305
+
304
306
if(users===undefined){
305
307
return<div></div>
306
308
}
@@ -336,9 +338,9 @@ export default connect(
336
338
)(withFirebase(MyComponent));
337
339
```
338
340
339
-
Here we unwatched the list on `componentWillUnmount`. We could also leave this away and the list will change in realtime in the background and it will not load all data on revisiting the Component again.
341
+
Here we unwatched the list on `componentWillUnmount`. We could also leave this away and the list will change in realtime in the background and it will not load all data on revisiting the Component again.
340
342
341
-
If you are using persistand watcher you would have to unwatch them in some point of your application or on application exit.
343
+
If you are using persistand watcher you would have to unwatch them in some point of your application or on application exit.
342
344
Because of that there are special calls that allow us to unwatch all persistand watcher in a single call. That could be calles in the root component of your application like this:
343
345
344
346
```js
@@ -351,7 +353,7 @@ Because of that there are special calls that allow us to unwatch all persistand
351
353
unwatchAllLists();
352
354
unwatchAllPaths();
353
355
}
354
-
356
+
355
357
//... other code of your root Component
356
358
357
359
```
@@ -370,12 +372,12 @@ The paths watcher exactly like the lists watcher with `watchPath` and `unwatchPa
370
372
const { watchPath }=this.props;
371
373
watchPath('users_count'); //Here we started watching the path
372
374
}
373
-
375
+
374
376
componentWillUnmount() {
375
377
const { unwatchPath, }=this.props;
376
378
unwatchPath('users'); // We can unwatch the path on unmounting the Component
377
379
}
378
-
380
+
379
381
//...
380
382
```
381
383
@@ -395,15 +397,15 @@ And comes the cool thing. If you are in the Form working on fields and someone e
0 commit comments