Reactfire
npm install reactfire
Hooks, Context Providers, and Components that make it easy to interact with Firebase.
By default, every reactfire hook throws a Promise until it has
connected to Firebase, allowing you to use Suspense to render a fallback component. If you don't want reactfire to throw a promise, pass an initial value to a reactfire hook. It will emit the initial value right away instead of throwing a promise.
Quickstart
Listen for realtime changes in a Firestore document with Reactfire. We'll use create-react-app to quickly get a Reactfire demo up and running.
-
Create a fresh React app.
create-react-app myapp -
Install reactfire and the Firebase SDK
npm i firebase reactfire -
Create a world-readable document in Firestore.
For example, a collection called tryreactfire with document burrito with field
yummy: true.To keep this as simple as possible, modify your security rules to make that document world-readable.
-
Modify
src/index.js-
Import firebase and reactfire
//... import { FirebaseAppProvider } from 'reactfire'; import * as firebase from 'firebase/app'; //... -
Wrap your app in a
FirebaseAppProviderand provide the config object from the Firebase console//... const firebaseConfig = { /* add your config object from Firebase console */ }; ReactDOM.render( <FirebaseAppProvider config={firebaseConfig}> <App /> </FirebaseAppProvider>, document.getElementById('root') ); //...
-
-
Modify
src/App.js-
Import
firebase/firestoreas well as theuseFirestoreDocanduseFirebaseApphooks//... import 'firebase/firestore'; import { UseFirestoreDoc } from 'reactfire'; //... -
Add a function component
//... function Burrito() { // create a ref const firebaseApp = useFirebaseApp(); const burritoRef = firebaseApp .firestore() .collection('tryreactfire') .doc('burrito'); // get the doc. just one line! const burritoDoc = useFirestoreDoc(burritoRef); // get the value from the doc const isYummy = burritoDoc.data().yummy; return <p>The burrito is {isYummy ? 'good' : 'bad'}</p>; } //... -
Render your component inside of a
Suspensetag
//... function App() { return ( <div className="App"> <header className="App-header">{/* ... */}</header> <Suspense fallback={'loading burrito status...'}> <Burrito /> </Suspense> </div> ); } //... -
-
Run your app!
npm run start -
Edit the value of
yummyin the Firebase console, and watch it update in real time in your app! 🔥🔥🔥
Docs
ToC
- Providers
- Hooks
useFirebaseApp- Authentication
- Database
- Cloud Firestore
- Realtime Database
- Cloud Storage
- Components
Providers
FirebaseAppProvider
Hooks
useFirebaseApp
useUser
useFirestoreDoc
useFirestoreCollection
useDatabaseObject
useDatabaseList
useStorageTask
useStorageDownloadURL
Components
SuspenseWithPerf
AuthCheck
AuthCheckProps
| Property | Type |
|---|---|
| auth | Auth |
| children | React.Component |
| fallback | React.Component |
| requiredClaims | Object |
SuspensePerfProps
| Property | Type |
|---|---|
| children | React.Component |
| fallback | React.Component |
| firePerf | any |
| traceId | string |
ReactFireOptions
| Property | Type |
|---|---|
| startWithValue | any |
Components
AuthCheck
Props
interface AuthCheckProps
Returns
React.FunctionComponent
SuspenseWithPerf
Props
interface SuspensePerfProps
Returns
React.FunctionComponent
Hooks
useDatabaseList
Subscribe to a Realtime Database list
Parameters
| Parameter | Type |
|---|---|
| ref | Reference or Query |
| options ? | ReactFireOptions |
Returns
QueryChange[]
useDatabaseObject
Subscribe to a Realtime Database object
Parameters
| Parameter | Type |
|---|---|
| ref | Reference |
| options ? | ReactFireOptions |
Returns
QueryChange
useFirestoreCollection
Subscribe to a Firestore collection
Parameters
| Parameter | Type |
|---|---|
| ref | CollectionReference |
| options ? | ReactFireOptions |
Returns
QuerySnapshot
useFirestoreDoc
Suscribe to Firestore Document changes
Parameters
| Parameter | Type |
|---|---|
| ref | DocumentReference |
| options ? | ReactFireOptions |
Returns
DocumentSnapshot
useStorageDownloadURL
Subscribe to a storage ref's download URL
Parameters
| Parameter | Type |
|---|---|
| ref | Reference |
| options ? | ReactFireOptions |
Returns
string
useStorageTask
Subscribe to the progress of a storage task
Parameters
| Parameter | Type |
|---|---|
| task | UploadTask |
| ref | Reference |
| options ? | ReactFireOptions |
Returns
UploadTaskSnapshot
useUser
Subscribe to Firebase auth state changes, including token refresh
Parameters
| Parameter | Type |
|---|---|
| auth | Auth |
| options ? | ReactFireOptions |
Returns
User
useObservable
Parameters
| Parameter | Type |
|---|---|
| observable$ | Observable |
| observableId | string |
| startWithValue ? | any |
Returns
any
For development
yarn installcdinto the reactfire/reactfire directory. runyarn run watch.- In a new terminal,
cdinto the reactfire/sample-simple directory. runyarn start. - Head over to https://localhost:3000 to see the running sample
Testing
cdinto the reactfire/reactfire directory- run
yarn test