Skip to content

Commit df5ecdf

Browse files
committed
[messaging] Add useToken hook
1 parent f2d4fc5 commit df5ecdf

File tree

8 files changed

+99
-4
lines changed

8 files changed

+99
-4
lines changed

auth/useAuthState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default (auth: Auth, options?: AuthStateOptions): AuthStateHook => {
3434
return () => {
3535
listener();
3636
};
37-
}, [auth]);
37+
}, [auth, options]);
3838

3939
const resArray: AuthStateHook = [value, loading, error];
4040
return useMemo<AuthStateHook>(() => resArray, resArray);

functions/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ All hooks can be imported from `react-firebase-hooks/functions`, e.g.
99
import { useHttpsCallable } from 'react-firebase-hooks/functions';
1010
```
1111

12-
List of Cloud Firestore hooks:
12+
List of Cloud Functions hooks:
1313

14-
- [React Firebase Hooks - Cloud Firestore](#react-firebase-hooks---cloud-firestore)
14+
- [React Firebase Hooks - Cloud Functions](#react-firebase-hooks---cloud-functions)
1515
- [useHttpsCallable](#usehttpscallable)
1616
- [Full example](#full-example)
1717

messaging/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# React Firebase Hooks - Cloud Messaging
2+
3+
React Firebase Hooks provides a convenience hook for getting a cloud messaging token, providing an `error` and `loading` property
4+
to give a complete lifecycle for accessing the cloud messaging token on Firebase Cloud Messaging.
5+
6+
All hooks can be imported from `react-firebase-hooks/messaging`, e.g.
7+
8+
```js
9+
import { useToken } from 'react-firebase-hooks/messaging';
10+
```
11+
12+
List of Cloud Messaging hooks:
13+
14+
- [React Firebase Hooks - Cloud Messaging](#react-firebase-hooks---cloud-messaging)
15+
- [useToken](#usetoken)
16+
- [Full example](#full-example)
17+
18+
### useToken
19+
20+
```js
21+
const [token, loading, error] = useToken(messaging);
22+
```
23+
24+
Get a token from Firebase Cloud Messaging
25+
26+
The `useToken` hook takes the following parameters:
27+
28+
- `messaging`: `messaging.Messaging` instance for your Firebase app
29+
30+
Returns:
31+
32+
- `token`: a `string` token to use with cloud messaging
33+
- `loading`: a `boolean` to indicate if the function is still being executed
34+
- `error`: Any `Error` returned by Firebase when trying to execute the function, or `undefined` if there is no error
35+
36+
#### Full example
37+
38+
```js
39+
import { getMessaging } from 'firebase/messaging';
40+
import { useToken } from 'react-firebase-hooks/messaging';
41+
42+
const MessagingToken = () => {
43+
const [token, loading, error] = useToken(getMessaging(firebaseApp));
44+
return (
45+
<div>
46+
<p>
47+
{error && <strong>Error: {JSON.stringify(error)}</strong>}
48+
{loading && <span>Loading token...</span>}
49+
{token && <span>Token:{token}</span>}
50+
</p>
51+
</div>
52+
);
53+
};
54+
```

messaging/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as useToken, TokenHook } from './useToken';

messaging/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "react-firebase-hooks/messaging",
3+
"main": "dist/index.cjs.js",
4+
"module": "dist/index.esm.js",
5+
"typings": "dist/functions/index.d.ts"
6+
}

messaging/useToken.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Messaging, getToken } from 'firebase/messaging';
2+
import { useEffect, useMemo } from 'react';
3+
import { LoadingHook, useLoadingValue } from '../util';
4+
5+
export type TokenHook = LoadingHook<string | null, Error>;
6+
7+
export default (messaging: Messaging): TokenHook => {
8+
const { error, loading, setError, setValue, value } = useLoadingValue<
9+
string | null,
10+
Error
11+
>();
12+
13+
useEffect(() => {
14+
getToken(messaging).then(setValue).catch(setError);
15+
}, [messaging]);
16+
17+
const resArray: TokenHook = [value, loading, error];
18+
return useMemo<TokenHook>(() => resArray, resArray);
19+
};

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
"functions/dist/util",
3535
"functions/dist/*.js.flow",
3636
"functions/package.json",
37+
"messaging/dist/*.js",
38+
"messaging/dist/functions",
39+
"messaging/dist/util",
40+
"messaging/dist/*.js.flow",
41+
"messaging/package.json",
3742
"storage/dist/*.js",
3843
"storage/dist/storage",
3944
"storage/dist/util",

rollup.config.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import authPkg from './auth/package.json';
1010
import databasePkg from './database/package.json';
1111
import firestorePkg from './firestore/package.json';
1212
import functionsPkg from './functions/package.json';
13+
import messagingPkg from './messaging/package.json';
1314
import storagePkg from './storage/package.json';
1415

1516
const pkgsByName = {
1617
auth: authPkg,
1718
database: databasePkg,
1819
firestore: firestorePkg,
1920
functions: functionsPkg,
21+
messaging: messagingPkg,
2022
storage: storagePkg,
2123
};
2224

@@ -35,10 +37,18 @@ const external = [
3537
'firebase/database',
3638
'firebase/firestore',
3739
'firebase/functions',
40+
'firebase/messaging',
3841
'firebase/storage',
3942
];
4043

41-
const components = ['auth', 'database', 'firestore', 'functions', 'storage'];
44+
const components = [
45+
'auth',
46+
'database',
47+
'firestore',
48+
'functions',
49+
'messaging',
50+
'storage',
51+
];
4252

4353
export default components
4454
.map((component) => {

0 commit comments

Comments
 (0)