|
| 1 | +# React Firebase Hooks - Cloud Functions |
| 2 | + |
| 3 | +React Firebase Hooks provides a convenience hook for HttpsCallable functions, providing an `error` and `loading` property |
| 4 | +to give a complete lifecycle for executing a HttpsCallable function on Firebase Cloud Functions. |
| 5 | + |
| 6 | +All hooks can be imported from `react-firebase-hooks/functions`, e.g. |
| 7 | + |
| 8 | +```js |
| 9 | +import { useHttpsCallable } from 'react-firebase-hooks/functions'; |
| 10 | +``` |
| 11 | + |
| 12 | +List of Cloud Firestore hooks: |
| 13 | + |
| 14 | +- [React Firebase Hooks - Cloud Firestore](#react-firebase-hooks---cloud-firestore) |
| 15 | + - [useHttpsCallable](#usehttpscallable) |
| 16 | + - [Full example](#full-example) |
| 17 | + |
| 18 | +### useHttpsCallable |
| 19 | + |
| 20 | +```js |
| 21 | +const [executeCallable, loading, error] = useHttpsCallable(functions, name); |
| 22 | +``` |
| 23 | + |
| 24 | +Generate a callable function and monitor its execution. |
| 25 | + |
| 26 | +The `useHttpsCallable` hook takes the following parameters: |
| 27 | + |
| 28 | +- `functions`: `functions.Functions` instance for your Firebase app |
| 29 | +- `name`: A `string` representing the name of the function to call |
| 30 | + |
| 31 | +Returns: |
| 32 | + |
| 33 | +- `executeCallable(data)`: a function you can call to execute the HttpsCallable |
| 34 | +- `loading`: a `boolean` to indicate if the function is still being executed |
| 35 | +- `error`: Any `Error` returned by Firebase when trying to execute the function, or `undefined` if there is no error |
| 36 | + |
| 37 | +#### Full example |
| 38 | + |
| 39 | +```js |
| 40 | +import { getFunctions } from 'firebase/functions'; |
| 41 | +import { useHttpsCallable } from 'react-firebase-hooks/functions'; |
| 42 | + |
| 43 | +const HttpsCallable = () => { |
| 44 | + const [executeCallable, executing, error] = useHttpsCallable( |
| 45 | + getFunctions(firebaseApp), |
| 46 | + 'myHttpsCallable' |
| 47 | + ); |
| 48 | + return ( |
| 49 | + <div> |
| 50 | + <p> |
| 51 | + {error && <strong>Error: {JSON.stringify(error)}</strong>} |
| 52 | + {executing && <span>Function executing...</span>} |
| 53 | + <button |
| 54 | + onClick={async () => { |
| 55 | + executeCallable(); |
| 56 | + alert('Executed function'); |
| 57 | + }} |
| 58 | + > |
| 59 | + Execute callable function |
| 60 | + </button> |
| 61 | + </p> |
| 62 | + </div> |
| 63 | + ); |
| 64 | +}; |
| 65 | +``` |
0 commit comments