Skip to content

Commit ff1c1ea

Browse files
authored
Add a StorageImage component (#182)
1 parent e8506dc commit ff1c1ea

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

docs/reference.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
- [`SuspenseWithPerf`](#SuspenseWithPerf)
2424
- Authentication
2525
- [`AuthCheck`](#AuthCheck)
26+
- Cloud Storage
27+
- [`StorageImage`](#StorageImage)
2628
- [ReactFireOptions](#ReactFireOptions)
2729

2830
## Providers
@@ -197,6 +199,19 @@ Renders `children` if a user is signed in and meets the required claims. Renders
197199
| fallback | React.Component |
198200
| requiredClaims | Object |
199201

202+
### `StorageImage`
203+
204+
Renders an image based on a Cloud Storage path.
205+
206+
#### Props
207+
208+
| Property | Type |
209+
| ----------- | ------------------------ |
210+
| storagePath | string |
211+
| storage? | firebase.storage.Storage |
212+
213+
...and any other props a normal React `<img>` element can take.
214+
200215
### `SuspenseWithPerf`
201216

202217
Starts a Firebase Performance Monitoring [trace](https://firebase.google.com/docs/reference/js/firebase.performance.Trace) and ends it when suspense stops suspending.

reactfire/storage/index.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import * as React from 'react';
12
import { storage } from 'firebase/app';
23
import { getDownloadURL } from 'rxfire/storage';
34
import { Observable } from 'rxjs';
4-
import { ReactFireOptions, useObservable } from '..';
5+
import { ReactFireOptions, useObservable, useFirebaseApp } from '..';
56

67
/**
78
* modified version of rxFire's _fromTask
@@ -59,3 +60,23 @@ export function useStorageDownloadURL<T = string>(
5960
options ? options.startWithValue : undefined
6061
);
6162
}
63+
64+
type StorageImageProps = {
65+
storagePath: string;
66+
storage?: firebase.storage.Storage;
67+
};
68+
69+
export function StorageImage(
70+
props: StorageImageProps &
71+
React.DetailedHTMLProps<
72+
React.ImgHTMLAttributes<HTMLImageElement>,
73+
HTMLImageElement
74+
>
75+
) {
76+
let { storage, storagePath, ...imgProps } = props;
77+
78+
storage = storage || useFirebaseApp().storage();
79+
80+
const imgSrc = useStorageDownloadURL(storage.ref(storagePath));
81+
return <img src={imgSrc} {...imgProps} />;
82+
}

sample-simple/src/Storage.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,12 @@ import '@firebase/performance';
44
import React, { useState } from 'react';
55
import {
66
SuspenseWithPerf,
7-
useStorageDownloadURL,
87
useStorageTask,
98
useFirebaseApp,
10-
AuthCheck
9+
AuthCheck,
10+
StorageImage
1111
} from 'reactfire';
1212

13-
const DownloadImage = () => {
14-
const demoImagePath = 'Cloud Storage for Firebase (Independent Icon).png';
15-
const firebaseApp = useFirebaseApp();
16-
const ref = firebaseApp.storage().ref(demoImagePath);
17-
18-
const downloadURL = useStorageDownloadURL(ref);
19-
20-
return (
21-
<img
22-
src={downloadURL}
23-
alt="demo download"
24-
style={{ width: '200px', height: '200px' }}
25-
/>
26-
);
27-
};
28-
2913
const UploadProgress = ({ uploadTask, storageRef }) => {
3014
const { bytesTransferred, totalBytes } = useStorageTask(
3115
uploadTask,
@@ -82,7 +66,11 @@ const SuspenseWrapper = props => {
8266
return (
8367
<SuspenseWithPerf fallback="loading..." traceId="storage-root">
8468
<AuthCheck fallback="sign in to use Storage">
85-
<DownloadImage />
69+
<StorageImage
70+
storagePath="Cloud Storage for Firebase (Independent Icon).png"
71+
alt="demo download"
72+
style={{ width: '200px', height: '200px' }}
73+
/>
8674
<br />
8775
<ImageUploadButton />
8876
</AuthCheck>

0 commit comments

Comments
 (0)