Skip to content

Commit 32c2bb3

Browse files
authored
[WIP] Docs updates 📚 (#208)
* add sdk init to docs * add profile page example * add Analytics example * address review comments * update root description * enable concurrent mode in initial sample * update quickstart * automatically fork on stackblitz * fix readme sample * remove contributing in favor of contributing.md * move docs links higher up * document useObservable and preloadFirestoreDoc * update usage docs * add useTransition docs * fix anchor links * polish * fix useobservable link * Address James and David's in-person feedback
1 parent c67dfa7 commit 32c2bb3

File tree

4 files changed

+257
-113
lines changed

4 files changed

+257
-113
lines changed

README.md

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,59 @@
1-
# Reactfire
1+
# ReactFire
22

33
Hooks, Context Providers, and Components that make it easy to interact with
44
Firebase.
55

6-
> If you're looking for docs for the _deprecated_ ReactFire v1 (the one that
7-
> uses mixins), click
8-
> [here](https://github.com/FirebaseExtended/reactfire/tree/v1.0.0)
9-
10-
**Status: Alpha**. ReactFire is meant for React Concurrent Mode, which is only
6+
⚠️ **Status: Experimental**. The API is intended to be stable, but ReactFire is meant for React Concurrent Mode, which is only
117
available in
128
[experimental React builds](https://reactjs.org/docs/concurrent-mode-adoption.html#installation).
139

1410
## What is ReactFire?
1511

16-
- **Easy realtime updates for your function components** - Reactfire's hooks,
17-
like `useFirestoreCollection` and `useUser`, let you easily subscribe to
18-
events, and automatically unsubscribe when your component unmounts.
19-
- **Loading states handled by `<Suspense>`** - Reactfire's hooks throw promises
12+
- **Easy realtime updates for your function components** - Hooks
13+
like `useUser`and `useFirestoreCollection` let you easily subscribe to
14+
auth state, realtime data, and all other Firebase SDK events. Plus, they automatically unsubscribe when your component unmounts.
15+
- **Loading states handled by `<Suspense>`** - ReactFire's hooks throw promises
2016
that Suspense can catch. No more `isLoaded ?...` - let React
21-
[handle it for you](https://reactjs.org/blog/2018/11/27/react-16-roadmap.html#react-166-shipped-the-one-with-suspense-for-code-splitting).
22-
- **Dead-simple Real User Monitoring (RUM)** - Easily enable Firebase
23-
Performance Monitoring's
24-
[automatic traces](https://firebase.google.com/docs/perf-mon/automatic-web),
25-
and instrument your Suspenseful loads with Reactfire's `<SuspenseWithPerf>`
26-
component
17+
[handle it for you](https://reactjs.org/docs/concurrent-mode-suspense.html).
18+
- **Faster initial page load times** - Load only the code you need, when you need it, with `useFirestore`, `useAuth`, `useRemoteConfig`, and more.
19+
- **Convenient components for common use cases** - Only want to render a component if a user is signed in? Wrap it in `<AuthCheck />`. Need to automatically instrument your `Suspense` load times with [RUM](https://firebase.google.com/docs/perf-mon)? Use `<SuspenseWithPef />`.
2720

2821
## Install
2922

3023
```bash
3124
# npm
32-
npm install --save reactfire
25+
npm install --save reactfire firebase
3326

3427
# yarn
35-
yarn add reactfire
28+
yarn add reactfire firebase
3629
```
3730

31+
- [**Quickstart**](./docs/quickstart.md)
32+
- [**Common Use Cases**](./docs/use.md)
33+
- [**API Reference**](./docs/reference.md)
34+
3835
## Example use
3936

4037
Check out the
41-
[live version on StackBlitz](https://stackblitz.com/edit/reactfire-sample)!
38+
[live version on StackBlitz](https://stackblitz.com/fork/reactfire-sample)!
4239

4340
```jsx
4441
import React, { Component } from 'react';
45-
import { render } from 'react-dom';
46-
import './style.css';
42+
import { createRoot } from 'react-dom';
4743
import {
4844
FirebaseAppProvider,
4945
useFirestoreDocData,
46+
useFirestore,
5047
SuspenseWithPerf
5148
} from 'reactfire';
5249

5350
const firebaseConfig = {
54-
/* add your config object from the Firebase console */
51+
/* Add your config from the Firebase Console */
5552
};
5653

5754
function Burrito() {
58-
// lazy load the Firestore SDK and create a ref
55+
// lazy load the Firestore SDK
56+
// and create a ref
5957
const burritoRef = useFirestore()
6058
.collection('tryreactfire')
6159
.doc('burrito');
@@ -65,18 +63,15 @@ function Burrito() {
6563
// and then streams live updates
6664
const burrito = useFirestoreDocData(burritoRef);
6765

68-
// get the value from the doc
69-
const isYummy = burrito.yummy;
70-
71-
return <p>The burrito is {isYummy ? 'good' : 'bad'}!</p>;
66+
return <p>The burrito is {burrito.yummy ? 'good' : 'bad'}!</p>;
7267
}
7368

7469
function App() {
7570
return (
76-
<FirebaseAppProvider firebaseConfig={firebaseConfig} initPerformance>
71+
<FirebaseAppProvider firebaseConfig={firebaseConfig}>
7772
<h1>🌯</h1>
7873
<SuspenseWithPerf
79-
fallback={'loading burrito status...'}
74+
fallback={<p>loading burrito status...</p>}
8075
traceId={'load-burrito-status'}
8176
>
8277
<Burrito />
@@ -85,30 +80,13 @@ function App() {
8580
);
8681
}
8782

88-
render(<App />, document.getElementById('root'));
83+
// Enable Concurrent Mode
84+
// https://reactjs.org/docs/concurrent-mode-adoption.html#enabling-concurrent-mode
85+
createRoot(document.getElementById('root')).render(<App />);
8986
```
9087

91-
## Learn More
92-
93-
- [**Quickstart**](./docs/quickstart.md)
94-
- [**Common Use Cases**](./docs/use.md)
95-
- [**API Reference**](./docs/reference.md)
96-
97-
## Contributing
98-
99-
### For development
88+
---
10089

101-
1. [Clone](https://help.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository)
102-
this repository (or a
103-
[fork](https://help.github.com/en/github/getting-started-with-github/fork-a-repo#propose-changes-to-someone-elses-project))
104-
1. At the project root, install all modules by running `yarn install`.
105-
1. `cd` into the _reactfire_ directory. Run `yarn` and `yarn watch`.
106-
1. In a new terminal, `cd` into the _reactfire/sample_ directory. run `yarn` and
107-
`yarn start`.
108-
1. Head over to https://localhost:3000 to see the running sample! If you edit
109-
the reactfire source, the sample will reload.
110-
111-
### Testing
112-
113-
1. `cd` into the _reactfire/reactfire_ directory
114-
1. run `yarn test`
90+
> If you're looking for docs for the _deprecated_ ReactFire v1 (the one that
91+
> uses mixins), click
92+
> [here](https://github.com/FirebaseExtended/reactfire/tree/v1.0.0)

docs/quickstart.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Reactfire Quickstart
1+
# ReactFire Quickstart
22

33
⚛ + 🔥 = 🌯
44

55
We'll build a web app that displays, in _real time_, the tastiness of a burrito. It will listen to **Cloud Firestore** for its data, and we'll configure **Firebase Performance Monitoring** so we can get some perf stats.
66

7-
To see the completed app, check out [this StackBlitz workspace](https://stackblitz.com/edit/reactfire-sample).
7+
To see the completed app, check out [this StackBlitz workspace](https://stackblitz.com/fork/reactfire-sample).
88

9-
## 1. In a terminal, create a fresh React app and `cd` into its directory.
9+
## 1. In a terminal, create a fresh React app and `cd` into its directory
1010

1111
> Prerequisite: make sure you have [Node.js](https://nodejs.org/en/) installed.
1212
@@ -15,7 +15,7 @@ npx create-react-app myapp
1515
cd myapp
1616
```
1717

18-
## 2. Install reactfire and the Firebase SDK
18+
## 2. Install ReactFire and the Firebase SDK
1919

2020
```bash
2121
# yarn
@@ -48,12 +48,11 @@ npm install --save firebase reactfire
4848

4949
## 4. Modify `src/index.js`
5050

51-
1. Import firebase and reactfire
51+
1. Import Firebase and ReactFire
5252

5353
```js
5454
//...
5555
import { FirebaseAppProvider } from 'reactfire';
56-
import 'firebase/performance';
5756
//...
5857
```
5958

@@ -64,11 +63,10 @@ npm install --save firebase reactfire
6463
const firebaseConfig = {
6564
/* add your config object from Firebase console */
6665
};
67-
ReactDOM.render(
68-
<FirebaseAppProvider firebaseConfig={firebaseConfig} initPerformance>
66+
ReactDOM.createRoot(document.getElementById('root')).render(
67+
<FirebaseAppProvider firebaseConfig={firebaseConfig}>
6968
<App />
70-
</FirebaseAppProvider>,
71-
document.getElementById('root')
69+
</FirebaseAppProvider>
7270
);
7371
//...
7472
```
@@ -88,11 +86,8 @@ npm install --save firebase reactfire
8886
```jsx
8987
//...
9088
function Burrito() {
91-
// lazy load the Firestore SDK
92-
const firestore = useFirestore();
93-
94-
// create a document reference
95-
const burritoRef = firestore()
89+
// lazy load the Firestore SDK and create a document reference
90+
const burritoRef = useFirestore()
9691
.collection('tryreactfire')
9792
.doc('burrito');
9893

@@ -118,6 +113,11 @@ Replace the `App` function with the following:
118113
function App() {
119114
return (
120115
<div className="App">
116+
{/*
117+
SuspenseWithPerf behaves the same as Suspense,
118+
but also automatically measures load times with the User Timing API
119+
and reports it to Firebase Performance Monitoring
120+
*/}
121121
<SuspenseWithPerf
122122
fallback={'loading burrito status...'}
123123
traceId={'load-burrito-status'}
@@ -140,11 +140,11 @@ npm run start
140140

141141
1. Edit the value of `yummy` in the Firebase console, and watch it update in real time in your app! 🔥🔥🔥
142142

143-
## _But what about Firebase Performance Monitoring?_
143+
## _About Firebase Performance Monitoring_
144144

145-
By passing the `initPerformance` prop to `FirebaseAppProvider`, our app will automatically measure [common performance stats](https://firebase.google.com/docs/perf-mon/automatic-web), as well as report on our custom trace, `load-burrito-status`, that we set in the `traceId` prop of `SuspenseWithPerf`.
145+
`SuspenseWithPerf` will lazy load the Firebase Performance Monitoring library and report on on our custom trace, `load-burrito-status`, that we set in the `traceId` prop of `SuspenseWithPerf`. In addition, it will automatically measure [common performance stats](https://firebase.google.com/docs/perf-mon/automatic-web)!
146146

147-
However, Firebase Performance Monitoring can take about 12 hours to crunch your data and show it in the _Performance_ tab of the Firebase console.
147+
Note that Firebase Performance Monitoring can take about 12 hours to crunch your data and show it in the _Performance_ tab of the Firebase console.
148148

149149
This is an example of some of the stats in the Firebase Performance Monitoring console after 12 hours:
150150

docs/reference.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- Cloud Storage
3636
- [`useStorageTask`](#useStorageTask)
3737
- [`useStorageDownloadURL`](#useStorageDownloadURL)
38+
- [`useObservable`](#useobservablet)
3839
- [ReactFireOptions](#ReactFireOptions)
3940

4041
- [Components](#Components)
@@ -56,6 +57,8 @@
5657
- [`preloadPerformance`](#preloadPerformance)
5758
- [`preloadRemoteConfig`](#preloadRemoteConfig)
5859
- [`preloadStorage`](#preloadStorage)
60+
- Data
61+
- [`preloadFirestoreDoc`](#preloadFirestoreDoc)
5962

6063
## Hooks
6164

@@ -368,6 +371,8 @@ _Throws a Promise by default_
368371

369372
Subscribe to a storage blob's download URL
370373

374+
useobservable link
375+
371376
_Throws a Promise by default_
372377

373378
### Parameters
@@ -381,6 +386,21 @@ _Throws a Promise by default_
381386

382387
`string`
383388

389+
### `useObservable<T>`
390+
391+
_Throws a Promise by default_
392+
393+
| Parameter | Type | Description |
394+
| ------------------ | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
395+
| source | `Observable` | The observable whose values you want to subscribe to |
396+
| observableId | `string` | A unique id. If this id matches an observable already in the cache, `useObservable` will reuse that observable instead of the one passed in. |
397+
| startWithValue _?_ | `T` | A value to emit first (if you don't want `useObservable` to throw a Promise) |
398+
| deps | React.DependencyList | A list of values that, when changed, should cause `useObservable` to re-subscribe to its observable |
399+
400+
##### Returns
401+
402+
`T`
403+
384404
## ReactFireOptions
385405

386406
| Property | Type |
@@ -578,3 +598,20 @@ Start importing the `firebase/storage` package.
578598
#### Returns
579599

580600
`Promise<`[`firebase.storage`](https://firebase.google.com/docs/reference/js/firebase.storage)`>`
601+
602+
### `preloadFirestoreDoc`
603+
604+
⚠️ experimental
605+
606+
Starts subscribing to a Firestore document in the background. Cleans itself up after 30 seconds if `useFirestoreDoc` calls are made.
607+
608+
#### Parameters
609+
610+
| Parameter | Type | Description |
611+
| ----------- | -------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
612+
| refProvider | `(firestore: firebase.firestore.Firestore) => firestore.DocumentReference` | A function that is called when the firestore SDK is ready and generates a DocumentReference to read |
613+
| firebaseApp | `firebase.app.App` | The firebase app |
614+
615+
#### Returns
616+
617+
`Promise<Obsevable<any>>`

0 commit comments

Comments
 (0)