@@ -98,38 +98,36 @@ Firestore queries can be created in the following ways:
9898` useFirestoreConnect ` is a React hook that manages attaching and detaching listeners for you as the component mounts and unmounts.
9999
100100#### Examples
101-
1021011 . 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+ ```
116114
1171152 . Props can be used as part of queries. In this case we will get a specific todo:
118116
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+ ```
133131
134132### Automatically with HOC {#firestoreConnect}
135133
@@ -138,36 +136,35 @@ export default function SomeComponent({ todoId }) {
138136#### Examples
139137
1401381 . 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+ ```
154152
1551532 . 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+ ```
171168
172169## Manual {#manual}
173170
@@ -281,58 +278,58 @@ By default the results of queries are stored in redux under the path of the quer
281278#### Examples
282279
2832801 . 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+ ```
334332
335333## Populate {#populate}
336334
337335Populate 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