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
Use the `$` as much as you want - it will only result in one Firebase read request. When all the subscriptions are removed, it will automatically unsubscribe.
85
+
Use the `$` as much as you want - it will only result in one Firebase read request. When all the subscriptions are removed, it will automatically unsubscribe.
73
86
74
87
```svelte
75
88
<script>
@@ -83,8 +96,7 @@ Use the `$` as much as you want - it will only result in one Firebase read reque
83
96
{$post?.title}
84
97
```
85
98
86
-
Or better yet, use the built in `Doc` and `Collection` components. See below.
87
-
99
+
Or better yet, use the built in `Doc` and `Collection` components for Firestore, or `Node` and `NodeList` components for Realtime Database. See below.
88
100
89
101
## Stores
90
102
@@ -110,15 +122,15 @@ Listen to the current user. Render UI conditionally based on the auth state:
110
122
111
123
### Firestore Stores
112
124
113
-
Subscribe to realtime data. The store will unsubscribe automatically to avoid unnecessary Firestore reads.
125
+
Subscribe to realtime data. The store will unsubscribe automatically to avoid unnecessary Firestore reads.
114
126
115
127
```svelte
116
128
<script>
117
129
import { docStore, collectionStore } from 'sveltefire';
@@ -166,8 +212,8 @@ Second, pass the server data as the `startWith` value to a store. This will bypa
166
212
// Data fetched via server
167
213
exportlet data:PageData;
168
214
169
-
// Just give the store a startWith value
170
-
const post =docStore(firestore, 'posts/test', data.post);
215
+
// Just give the store a startWith value
216
+
const post =docStore(firestore, "posts/test", data.post);
171
217
```
172
218
173
219
## Realtime Components
@@ -176,17 +222,18 @@ In addition to stores, SvelteFire provides a set of components that can build co
176
222
177
223
### FirebaseApp
178
224
179
-
The `FirebaseApp` component puts the FirebaseSDK into Svelte context. This avoids the need to pass `auth`and `firestore` down to every component. It is typically placed in root layout.
225
+
The `FirebaseApp` component puts the FirebaseSDK into Svelte context. This avoids the need to pass `auth`, `firestore`and `rtdb` down to every component. It is typically placed in root layout.
180
226
181
227
```svelte
182
228
<!-- +layout.svelte -->
183
229
<script>
184
230
// Initialize Firebase...
185
-
const db = getFirestore(app);
231
+
const firestore = getFirestore(app);
232
+
const rtdb = getDatabase(app);
186
233
const auth = getAuth(app);
187
234
</script>
188
235
189
-
<FirebaseApp {auth} {firestore}>
236
+
<FirebaseApp {auth} {firestore} {rtdb} >
190
237
191
238
<User let:user></User>
192
239
<!-- other sveltefire components here -->
@@ -199,13 +246,13 @@ You can use Svelte's context API to access the Firebase SDK in any component.
Firestore components can also handle loading states:
241
-
288
+
242
289
```svelte
243
290
<Doc path="posts/test">
244
291
<!-- data renders here in the default slot -->
@@ -252,10 +299,9 @@ Pass a `startWith` value to bypass the loading state. This is useful in SvelteKi
252
299
<Doc ref="posts/test" startWith={dataFromServer}>
253
300
```
254
301
255
-
256
302
### Collection
257
303
258
-
Collections provides array of objects containing the document data, as well as the `id` and `ref` for each result. It also provides a `count` slot prop for number of docs in the query.
304
+
Collections provides array of objects containing the document data, as well as the `id` and `ref` for each result. It also provides a `count` slot prop for number of docs in the query.
259
305
260
306
```svelte
261
307
<Collection ref="posts" let:data let:count>
@@ -279,22 +325,71 @@ Collections can also take a Firestore Query instead of a path:
279
325
</Collection>
280
326
```
281
327
328
+
### Node
329
+
330
+
Fetch a single node from the Realtime Database and listen to its data in realtime. The `data` slot prop gives you access to the fetched data, and the `ref` provides the Realtime Database reference.
Fetch lists of nodes from the Realtime Database and listen to their data in realtime. The component provides an array of the data with the `data` slot prop, the reference with `ref`, and the `count` of items in the list with count.
366
+
367
+
```svelte
368
+
<NodeList path="posts" let:data let:count>
369
+
<p>Fetched {count} posts</p>
370
+
{#each data as post}
371
+
{item.nodeKey}
372
+
{post.content}
373
+
{/each}
374
+
</NodeList>
375
+
```
376
+
282
377
### Using Components Together
283
378
284
379
These components can be combined to build complex realtime apps. It's especially powerful when fetching data that requires the current user's UID or a related document's path.
0 commit comments