|
| 1 | +--- |
| 2 | +title: useConnectMutation |
| 3 | +--- |
| 4 | + |
| 5 | +`useConnectMutation` is a hook designed to simplify handling mutations (creating, updating, deleting) with Firebase Data Connect. |
| 6 | +This hook integrates with [Firebase Data Connect](https://firebase.google.com/docs/data-connect), which uses GraphQL to interact with a PostgreSQL database (via Cloud SQL) for secure, scalable data operations. |
| 7 | +The hook manages the mutation process and provides an easy-to-use interface to manage loading, success, and error states in your React application. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- Simplifies mutation handling for <b>create</b>, <b>update</b>, and <b>delete</b> operations using Firebase Data Connect. |
| 12 | +- Provides <b>type-safe</b> handling of mutations based on your Firebase Data Connect schema. |
| 13 | +- Automatically manages <b>loading</b>, <b>success</b>, and <b>error</b> states for mutations. |
| 14 | +- Supports <b>optimistic updates</b> and <b>caching</b> to improve user experience and performance. |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +```jsx |
| 19 | +import { useConnectQuery } from "@tanstack-query-firebase/react"; |
| 20 | +import { createMovieRef } from "@your-package-name/your-connector"; |
| 21 | + |
| 22 | +function Component() { |
| 23 | + const { mutate, isPending, isSuccess, isError, error } = |
| 24 | + useConnectMutation(createMovieRef); |
| 25 | + |
| 26 | + const handleSubmit = async (movieData) => { |
| 27 | + try { |
| 28 | + await mutate(movieData); |
| 29 | + } catch (err) { |
| 30 | + console.error("Failed to add movie:", err); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + if (isPending) return <div>Adding movie...</div>; |
| 35 | + if (isError) return <div>Error: {error.message}</div>; |
| 36 | + |
| 37 | + return ( |
| 38 | + <div> |
| 39 | + {isSuccess && <div>Movie added successfully!</div>} |
| 40 | + <form onSubmit={(e) => handleSubmit(e.target)}> |
| 41 | + {/* Form fields for movie data */} |
| 42 | + <button type="submit" disabled={isPending}> |
| 43 | + Add Movie |
| 44 | + </button> |
| 45 | + </form> |
| 46 | + </div> |
| 47 | + ); |
| 48 | +} |
| 49 | +``` |
0 commit comments