Skip to content

Commit 242df90

Browse files
authored
Merge pull request #132 from HassanBahati/docs-useConnectQuery-useConnectMutation
docs: add useConnectQuery and useConnectMutation
2 parents 1b02a00 + 2f4e6ed commit 242df90

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

docs.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@
4848
}
4949
]
5050
},
51+
{
52+
"tab": "react",
53+
"group": "Data Connect",
54+
"pages": [
55+
{
56+
"href": "/react/hooks/data-connect/useConnectQuery",
57+
"title": "useConnectQuery"
58+
},
59+
{
60+
"href": "/react/hooks/data-connect/useConnectMutation",
61+
"title": "useConnectMutation"
62+
}
63+
]
64+
},
5165
{
5266
"tab": "react",
5367
"group": "Firestore",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: useConnectQuery
3+
---
4+
5+
`useConnectQuery` is a hook designed to simplify data fetching and state management with Firebase Data Connect.
6+
This hook integrates with [Firebase Data Connect](https://firebase.google.com/docs/data-connect), which leverages GraphQL to interact with a PostgreSQL database (via Cloud SQL) for secure, scalable data operations.
7+
The hook simplifies the process of querying data from Firebase Data Connect and provides an easy-to-use interface to manage loading, success, and error states in your React application.
8+
9+
## Features
10+
11+
- Provides <b>type-safe</b> handling of queries based on the Firebase Data Connect schema.
12+
- Simplifies data fetching using Firebase Data Connect.
13+
- Automatically manages <b>loading</b>, <b>success</b>, and <b>error</b> states.
14+
- Supports <b>refetching data</b> with integrated <b>caching</b>.
15+
16+
## Usage
17+
18+
```jsx
19+
import { useConnectQuery } from "@tanstack-query-firebase/react";
20+
import { listMoviesQuery } from "@your-package-name/your-connector";
21+
22+
function Component() {
23+
const { data, isPending, isSuccess, isError, error } = useConnectQuery(
24+
listMoviesQuery()
25+
);
26+
27+
if (isPending) return <div>Loading...</div>;
28+
if (isError) return <div>Error: {error.message}</div>;
29+
30+
return (
31+
<div>
32+
{isSuccess && (
33+
<ul>
34+
{data.movies.map((movie) => (
35+
<li key={movie.id}>{movie.title}</li>
36+
))}
37+
</ul>
38+
)}
39+
</div>
40+
);
41+
}
42+
```

0 commit comments

Comments
 (0)