Skip to content

Commit f632250

Browse files
author
Scott Prue
committed
fix(hooks): remove unnessesary create functions from useFirebase and useFirestore
1 parent ed7790f commit f632250

File tree

7 files changed

+43
-118
lines changed

7 files changed

+43
-118
lines changed

docs/api/useFirebase.md

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,8 @@
22

33
### Table of Contents
44

5-
- [createUseFirebase](#createusefirebase)
65
- [useFirebase](#usefirebase)
76

8-
## createUseFirebase
9-
10-
Function that creates a react hook which provides `firebase` object.
11-
12-
**WARNING!!** This is an advanced feature, and should only be used when
13-
needing to access a firebase instance created under a different store key.
14-
Firebase state (`state.firebase`)
15-
16-
**Examples**
17-
18-
_Basic_
19-
20-
```javascript
21-
import { createUseFirebase } from 'react-redux-firebase'
22-
23-
// create useFirebase
24-
const useFirebase = createUseFirebase()
25-
```
26-
27-
Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** A hook fucntion that return firebase object.
28-
297
## useFirebase
308

319
React hook that provides `firebase` object.
@@ -44,14 +22,20 @@ import { useFirebase } from 'react-redux-firebase'
4422

4523
function AddData() {
4624
const firebase = useFirebase()
25+
26+
function addTodo() {
27+
const exampleTodo = { done: false, text: 'Sample' }
28+
return firebase.push('todos', exampleTodo)
29+
}
30+
4731
return (
4832
<div>
49-
<button onClick={() => firebase.push('todos', { done: false, text: 'Sample' })}>
33+
<button onClick={addTodo}>
5034
Add Sample Todo
5135
</button>
5236
</div>
5337
)
5438
}
5539
```
5640

57-
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Firebase instance
41+
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Extended Firebase instance

docs/api/useFirestore.md

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,10 @@
22

33
### Table of Contents
44

5-
- [createUseFirestore](#createusefirestore)
65
- [useFirestore](#usefirestore)
76

8-
## createUseFirestore
9-
10-
Function that creates a hook that which provides
11-
`firestore` object.
12-
13-
**WARNING!!** This is an advanced feature, and should only be used when
14-
needing to access a firebase instance created under a different store key.
15-
16-
**Parameters**
17-
18-
- `storeKey` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Name of redux store which contains
19-
Firestore state (`state.firestore`) (optional, default `'store'`)
20-
21-
**Examples**
22-
23-
_Basic_
24-
25-
```javascript
26-
import { createUseFirestore } from 'react-redux-firebase'
27-
28-
// create useFirestore that uses another redux store
29-
const useFirestore = createUseFirestore()
30-
31-
// use the useFirestore to wrap a component
32-
export default useFirestore(SomeComponent)
33-
```
34-
35-
Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Higher Order Component which accepts an array of
36-
watchers config and wraps a React Component
37-
387
## useFirestore
398

40-
**Extends React.Component**
41-
429
React hook that return firestore object.
4310
Firestore instance is gathered from `store.firestore`, which is attached
4411
to store by the store enhancer (`reduxFirestore`) during setup of
@@ -54,12 +21,15 @@ import { useFirestore } from 'react-redux-firebase'
5421

5522
function AddData({ firebase: { add } }) {
5623
const firestore = useFirestore()
57-
const add = todo => {
58-
firestore.collection('todos').add(todo)
24+
25+
function addTodo() {
26+
const exampleTodo = { done: false, text: 'Sample' }
27+
return firestore.collection('todos').add(exampleTodo)
5928
}
29+
6030
return (
6131
<div>
62-
<button onClick={() => add({ done: false, text: 'Sample' })}>
32+
<button onClick={addTodo}>
6333
Add Sample Todo
6434
</button>
6535
</div>
@@ -69,4 +39,4 @@ function AddData({ firebase: { add } }) {
6939
export default AddTodo
7040
```
7141

72-
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Firestore instance
42+
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Extended Firestore instance

docs/api/useFirestoreConnect.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
## createUseFirestoreConnect
99

1010
React hook that automatically listens/unListens to provided
11-
firebase paths.
11+
firestore paths.
1212
**WARNING!!** This is an advanced feature, and should only be used when
1313
needing to access a firebase instance created under a different store key.
14-
Firebase state (state.firebase)
14+
Firebase state (state.firestore)
1515

1616
**Examples**
1717

1818
_Basic_
1919

2020
```javascript
21-
// props.firebase set on App component as firebase object with helpers
21+
// props.firestore set on App component as firestore object with helpers
2222
import { createUseFirestoreConnect } from 'react-redux-firebase'
2323

2424
const firestoreConnect = createUseFirestoreConnect()

src/firebaseConnect.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const createFirebaseConnect = (storeKey = 'store') => (
6161
const { firebase, dispatch } = this.props
6262
const inputAsFunc = createCallable(dataOrFn)
6363
const data = inputAsFunc(np, this.store)
64+
6465
// Handle a data parameter having changed
6566
if (!isEqual(data, this.prevData)) {
6667
const itemsToSubscribe = differenceWith(data, this.prevData, isEqual)

src/firestoreConnect.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const createFirestoreConnect = (storeKey = 'store') => (
6868

6969
// Remove listeners for inactive subscriptions
7070
firestore.unsetListeners(changes.removed)
71+
7172
// Add listeners for new subscriptions
7273
firestore.setListeners(changes.added)
7374
}
@@ -86,7 +87,7 @@ export const createFirestoreConnect = (storeKey = 'store') => (
8687
}
8788

8889
FirestoreConnectWrapped.propTypes = {
89-
dispatch: PropTypes.func,
90+
dispatch: PropTypes.func.isRequired,
9091
firebase: PropTypes.object,
9192
firestore: PropTypes.object
9293
}

src/useFirebase.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
import { useContext } from 'react'
22
import ReactReduxFirebaseContext from './ReactReduxFirebaseContext'
33

4-
/**
5-
* @name createUseFirebase
6-
* @description Function that creates a react hook which provides `firebase` object.
7-
*
8-
* **WARNING!!** This is an advanced feature, and should only be used when
9-
* needing to access a firebase instance created under a different store key.
10-
* Firebase state (`state.firebase`)
11-
* @return {Function} - A hook fucntion that return firebase object.
12-
* @example <caption>Basic</caption>
13-
* import { createUseFirebase } from 'react-redux-firebase'
14-
*
15-
* // create useFirebase
16-
* const useFirebase = createUseFirebase()
17-
*/
18-
export const createUseFirebase = () =>
19-
function useFirebase() {
20-
return useContext(ReactReduxFirebaseContext)
21-
}
22-
234
/**
245
* @name useFirebase
256
* @description React hook that provides `firebase` object.
@@ -28,19 +9,28 @@ export const createUseFirebase = () =>
289
* **NOTE**: This version of the Firebase library has extra methods, config,
2910
* and functionality which give it it's capabilities such as dispatching
3011
* actions.
31-
* @return {Object} - Firebase instance
12+
* @return {Object} - Extended Firebase instance
3213
* @example <caption>Basic</caption>
3314
* import { useFirebase } from 'react-redux-firebase'
3415
*
3516
* function AddData() {
3617
* const firebase = useFirebase()
18+
*
19+
* function addTodo() {
20+
* const exampleTodo = { done: false, text: 'Sample' }
21+
* return firebase.push('todos', exampleTodo)
22+
* }
23+
*
3724
* return (
3825
* <div>
39-
* <button onClick={() => firebase.push('todos', { done: false, text: 'Sample' })}>
26+
* <button onClick={addTodo}>
4027
* Add Sample Todo
4128
* </button>
4229
* </div>
4330
* )
4431
* }
4532
*/
46-
export default createUseFirebase()
33+
34+
export default function useFirebase() {
35+
return useContext(ReactReduxFirebaseContext)
36+
}

src/useFirestore.js

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,28 @@
11
import { useContext } from 'react'
22
import ReduxFirestoreContext from './ReduxFirestoreContext'
33

4-
/**
5-
* @name createUseFirestore
6-
* @description Function that creates a hook that which provides
7-
* `firestore` object.
8-
*
9-
* **WARNING!!** This is an advanced feature, and should only be used when
10-
* needing to access a firebase instance created under a different store key.
11-
* @param {String} [storeKey='store'] - Name of redux store which contains
12-
* Firestore state (`state.firestore`)
13-
* @return {Function} - Higher Order Component which accepts an array of
14-
* watchers config and wraps a React Component
15-
* @example <caption>Basic</caption>
16-
* import { createUseFirestore } from 'react-redux-firebase'
17-
*
18-
* // create useFirestore that uses another redux store
19-
* const useFirestore = createUseFirestore()
20-
*
21-
* // use the useFirestore to wrap a component
22-
* export default useFirestore(SomeComponent)
23-
*/
24-
export const createUseFirestore = () =>
25-
function useFirestore() {
26-
return useContext(ReduxFirestoreContext)
27-
}
28-
294
/**
305
* @name useFirestore
31-
* @extends React.Component
326
* @description React hook that return firestore object.
337
* Firestore instance is gathered from `store.firestore`, which is attached
348
* to store by the store enhancer (`reduxFirestore`) during setup of
359
* [`redux-firestore`](https://github.com/prescottprue/redux-firestore)
36-
* @return {Object} - Firestore instance
10+
* @return {Object} - Extended Firestore instance
3711
* @example <caption>Basic</caption>
3812
* import React from 'react'
3913
* import { useFirestore } from 'react-redux-firebase'
4014
*
4115
* function AddData({ firebase: { add } }) {
4216
* const firestore = useFirestore()
43-
* const add = todo => {
44-
* firestore.collection('todos').add(todo)
17+
*
18+
* function addTodo() {
19+
* const exampleTodo = { done: false, text: 'Sample' }
20+
* return firestore.collection('todos').add(exampleTodo)
4521
* }
22+
*
4623
* return (
4724
* <div>
48-
* <button onClick={() => add({ done: false, text: 'Sample' })}>
25+
* <button onClick={addTodo}>
4926
* Add Sample Todo
5027
* </button>
5128
* </div>
@@ -54,4 +31,6 @@ export const createUseFirestore = () =>
5431
*
5532
* export default AddTodo
5633
*/
57-
export default createUseFirestore()
34+
export default function useFirestore() {
35+
return useContext(ReduxFirestoreContext)
36+
}

0 commit comments

Comments
 (0)