@@ -98,38 +98,36 @@ Firestore queries can be created in the following ways:
98
98
` useFirestoreConnect ` is a React hook that manages attaching and detaching listeners for you as the component mounts and unmounts.
99
99
100
100
#### Examples
101
-
102
101
1 . Basic query that will attach/detach as the component passed mounts/unmounts. In this case we are setting a listener for the ` 'todos' ` collection:
103
-
104
- ``` js
105
- import React from ' react'
106
- import { useSelector } from ' react-redux'
107
- import { useFirestoreConnect } from ' react-redux-firebase'
108
-
109
- export default function SomeComponent () {
110
- useFirestoreConnect ([
111
- { collection: ' todos' } // or 'todos'
112
- ])
113
- const todos = useSelector ((state ) => state .firestore .ordered .todos )
114
- }
115
- ```
102
+ ``` js
103
+ import React from ' react'
104
+ import { useSelector } from ' react-redux'
105
+ import { useFirestoreConnect } from ' react-redux-firebase'
106
+
107
+ export default function SomeComponent () {
108
+ useFirestoreConnect ([
109
+ { collection: ' todos' } // or 'todos'
110
+ ])
111
+ const todos = useSelector ((state ) => state .firestore .ordered .todos )
112
+ }
113
+ ```
116
114
117
115
2 . Props can be used as part of queries. In this case we will get a specific todo:
118
116
119
- ``` js
120
- import React from ' react'
121
- import { useSelector } from ' react-redux'
122
- import { useFirestoreConnect } from ' react-redux-firebase'
123
-
124
- export default function SomeComponent ({ todoId }) {
125
- useFirestoreConnect (() => [
126
- { collection: ' todos' , doc: todoId } // or `todos/${props.todoId}`
127
- ])
128
- const todo = useSelector (
129
- ({ firestore: { data } }) => data .todos && data .todos [todoId]
130
- )
131
- }
132
- ```
117
+ ``` js
118
+ import React from ' react'
119
+ import { useSelector } from ' react-redux'
120
+ import { useFirestoreConnect } from ' react-redux-firebase'
121
+
122
+ export default function SomeComponent ({ todoId }) {
123
+ useFirestoreConnect (() => [
124
+ { collection: ' todos' , doc: todoId } // or `todos/${props.todoId}`
125
+ ])
126
+ const todo = useSelector (
127
+ ({ firestore: { data } }) => data .todos && data .todos [todoId]
128
+ )
129
+ }
130
+ ```
133
131
134
132
### Automatically with HOC {#firestoreConnect}
135
133
@@ -138,36 +136,35 @@ export default function SomeComponent({ todoId }) {
138
136
#### Examples
139
137
140
138
1 . Basic query that will attach/detach as the component passed mounts/unmounts. In this case we are setting a listener for the ` 'todos' ` collection:
141
-
142
- ``` js
143
- import { compose } from ' redux'
144
- import { connect } from ' react-redux'
145
- import { firestoreConnect } from ' react-redux-firebase'
146
-
147
- export default compose (
148
- firestoreConnect (() => [' todos' ]), // or { collection: 'todos' }
149
- connect ((state , props ) => ({
150
- todos: state .firestore .ordered .todos
151
- }))
152
- )(SomeComponent)
153
- ```
139
+
140
+ ``` js
141
+ import { compose } from ' redux'
142
+ import { connect } from ' react-redux'
143
+ import { firestoreConnect } from ' react-redux-firebase'
144
+
145
+ export default compose (
146
+ firestoreConnect (() => [' todos' ]), // or { collection: 'todos' }
147
+ connect ((state , props ) => ({
148
+ todos: state .firestore .ordered .todos
149
+ }))
150
+ )(SomeComponent)
151
+ ```
154
152
155
153
2 . Create a query based on props by passing a function. In this case we will get a specific todo:
156
-
157
- ``` js
158
- import { compose } from ' redux'
159
- import { connect } from ' react-redux'
160
- import { firestoreConnect } from ' react-redux-firebase'
161
-
162
- export default compose (
163
- firestoreConnect ((props ) => [
164
- { collection: ' todos' , doc: props .todoId } // or `todos/${props.todoId}`
165
- ]),
166
- connect (({ firestore: { data } }, props ) => ({
167
- todos: data .todos && data .todos [todoId]
168
- }))
169
- )(SomeComponent)
170
- ```
154
+ ``` js
155
+ import { compose } from ' redux'
156
+ import { connect } from ' react-redux'
157
+ import { firestoreConnect } from ' react-redux-firebase'
158
+
159
+ export default compose (
160
+ firestoreConnect ((props ) => [
161
+ { collection: ' todos' , doc: props .todoId } // or `todos/${props.todoId}`
162
+ ]),
163
+ connect (({ firestore: { data } }, props ) => ({
164
+ todos: data .todos && data .todos [todoId]
165
+ }))
166
+ )(SomeComponent)
167
+ ```
171
168
172
169
## Manual {#manual}
173
170
@@ -281,58 +278,58 @@ By default the results of queries are stored in redux under the path of the quer
281
278
#### Examples
282
279
283
280
1 . Querying the same path with different query parameters
284
-
285
- ``` js
286
- import { compose } from ' redux'
287
- import { connect } from ' react-redux'
288
- import { firestoreConnect } from ' react-redux-firebase'
289
- const myProjectsReduxName = ' myProjects'
290
-
291
- compose (
292
- firestoreConnect ((props ) => [
293
- { collection: ' projects' },
294
- {
295
- collection: ' projects' ,
296
- where: [[' uid' , ' ==' , ' 123' ]],
297
- storeAs: myProjectsReduxName
298
- }
299
- ]),
300
- connect ((state , props ) => ({
301
- projects: state .firestore .data .projects ,
302
- myProjects: state .firestore .data [myProjectsReduxName] // use storeAs path to gather from redux
303
- }))
304
- )
305
- ```
306
-
307
- 2 . Set ` useFirestoreConnect ` for subcollections documents
308
- For example, in Firestore cloud you have such message structure:
309
- ` chatMessages (collection) / chatID (document) / messages (collection) / messageID (document) `
310
-
311
- You can't write the path in ` useFirestoreConnect ` like:
312
-
313
- ``` js
314
- useFirestoreConnect (` chatMessages/${ chatID} /messages` )
315
- ```
316
-
317
- You will have error:
318
-
319
- ` Queries with subcollections must use "storeAs" to prevent invalid store updates. This closley matches the upcoming major release (v1), which stores subcollections at the top level by default. `
320
-
321
- Solution:
322
- Use ` subcollections ` for 'messages' and ` storeAs ` .
323
-
324
- ```` import { useFirestoreConnect } from 'react-redux-firebase'
325
- useFirestoreConnect([
326
- {
327
- collection: 'chatMessages',
328
- doc: chatID,
329
- subcollections: [{ collection: 'messages' }],
330
- storeAs: 'myMessages'
331
- }
332
- ])```
333
-
281
+ ``` js
282
+ import { compose } from ' redux'
283
+ import { connect } from ' react-redux'
284
+ import { firestoreConnect } from ' react-redux-firebase'
285
+ const myProjectsReduxName = ' myProjects'
286
+
287
+ compose (
288
+ firestoreConnect ((props ) => [
289
+ { collection: ' projects' },
290
+ {
291
+ collection: ' projects' ,
292
+ where: [' uid' , ' ==' , ' 123' ],
293
+ storeAs: myProjectsReduxName
294
+ }
295
+ ]),
296
+ connect ((state , props ) => ({
297
+ projects: state .firestore .data .projects ,
298
+ myProjects: state .firestore .data [myProjectsReduxName] // use storeAs path to gather from redux
299
+ }))
300
+ )
301
+ ```
302
+
303
+ 2 . Set ` useFirestoreConnect ` for subcollections documents. For example, in Cloud Firestore you might have a message structure such as:
304
+
305
+ ` chatMessages (collection) / chatID (document) / messages (collection) / messageID (document) `
306
+
307
+ You cannot write the path in ` useFirestoreConnect ` like:
308
+
309
+ ``` js
310
+ useFirestoreConnect (` chatMessages/${ chatID} /messages` )
311
+ ```
312
+
313
+ This will lead to the error:
314
+
315
+ ` Queries with subcollections must use "storeAs" to prevent invalid store updates. This closley matches the upcoming major release (v1), which stores subcollections at the top level by default. `
316
+
317
+ ** Solution** :
318
+
319
+ Use ` subcollections ` for ` messages ` and ` storeAs ` .
320
+
321
+ ``` js
322
+ import { useFirestoreConnect } from ' react-redux-firebase'
323
+ useFirestoreConnect ([
324
+ {
325
+ collection: ' chatMessages' ,
326
+ doc: chatID,
327
+ subcollections: [{ collection: ' messages' }],
328
+ storeAs: ' myMessages'
329
+ }
330
+ ])
331
+ ```
334
332
335
333
## Populate {#populate}
336
334
337
335
Populate is supported for Firestore as of [ v0.6.0 of redux-firestore] ( https://github.com/prescottprue/redux-firestore/releases/tag/v0.6.0 ) . It was added [ as part of issue #48 ] ( https://github.com/prescottprue/redux-firestore/issues/48 ) .
338
- ````
0 commit comments