Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions examples/react/kitchen-sink/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# TanStack Query Firebase Examples

A comprehensive example application showcasing various TanStack Query Firebase hooks and patterns.

## Features

- **Authentication Examples**: ID token management with `useGetIdTokenQuery`
- **Firestore Examples**: Collection querying with `useCollectionQuery`
- **Real-time Updates**: See how the UI updates when data changes
- **Mutation Integration**: Add/delete operations with proper error handling
- **Loading States**: Proper loading and error state management
- **Query Key Management**: Dynamic query keys based on filters

## Running the Examples

1. Start the Firebase emulators:
```bash
cd ../../../ && firebase emulators:start
```

2. In another terminal, run the example app:
```bash
pnpm dev:emulator
```

3. Navigate to different examples using the navigation bar:
- **Home**: Overview of available examples
- **ID Token Query**: Firebase Authentication token management
- **Collection Query**: Firestore collection querying with filters

## Key Concepts Demonstrated

- Using `useGetIdTokenQuery` for Firebase Authentication
- Using `useCollectionQuery` with different query configurations
- Combining queries with mutations (`useAddDocumentMutation`, `useDeleteDocumentMutation`)
- Dynamic query keys for filtered results
- Proper TypeScript integration with Firestore data
- React Router for navigation between examples

## File Structure

```
src/
├── components/
│ ├── IdTokenExample.tsx # Authentication example
│ └── CollectionQueryExample.tsx # Firestore example
├── App.tsx # Main app with routing
├── firebase.ts # Firebase initialization
├── main.tsx # Entry point
└── index.css # Tailwind CSS
```

## Technologies Used

- **Vite**: Fast build tool and dev server
- **React Router**: Client-side routing
- **TanStack Query**: Data fetching and caching
- **Firebase**: Authentication and Firestore
- **Tailwind CSS**: Utility-first styling
- **TypeScript**: Type safety
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<title>TanStack Query Firebase Examples</title>
</head>
<body>
<div id="root"></div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "useGetIdTokenQuery",
"name": "firebase-examples",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"dev:emulator": "cd ../../../ && firebase emulators:exec --project test-project 'cd examples/react/useGetIdTokenQuery && vite'",
"dev:emulator": "cd ../../../ && firebase emulators:exec --project test-project 'cd examples/react/firebase-examples && vite'",
Copy link
Preview

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script path references 'firebase-examples' but the directory is actually 'kitchen-sink'. This will cause the emulator script to fail when trying to change to the correct directory.

Suggested change
"dev:emulator": "cd ../../../ && firebase emulators:exec --project test-project 'cd examples/react/firebase-examples && vite'",
"dev:emulator": "cd ../../../ && firebase emulators:exec --project test-project 'cd examples/react/kitchen-sink && vite'",

Copilot uses AI. Check for mistakes.

"build": "npx vite build",
"preview": "vite preview"
},
Expand All @@ -15,7 +15,8 @@
"@tanstack/react-query-devtools": "^5.84.2",
"firebase": "^11.3.1",
"react": "^19.1.1",
"react-dom": "^19.1.1"
"react-dom": "^19.1.1",
"react-router-dom": "^6.28.0"
},
"devDependencies": {
"@types/react": "^19.1.9",
Expand Down
181 changes: 181 additions & 0 deletions examples/react/kitchen-sink/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { useState } from "react";
import { Link, Route, Routes, useLocation } from "react-router-dom";
import { CollectionQueryExample } from "./components/CollectionQueryExample";
import { IdTokenExample } from "./components/IdTokenExample";
import { NestedCollectionsExample } from "./components/NestedCollectionsExample";
import { WithConverterExample } from "./components/WithConverterExample";

import "./firebase";

function App() {
const [queryClient] = useState(
() =>
new QueryClient({
defaultOptions: {
queries: {
staleTime: 60 * 1000,
},
},
}),
);

return (
<QueryClientProvider client={queryClient}>
<div className="min-h-screen bg-gray-50">
<Navigation />
<div className="py-12">
<div className="max-w-6xl mx-auto px-4">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/auth/id-token" element={<IdTokenExample />} />
<Route
path="/firestore/collection-query"
element={<CollectionQueryExample />}
/>
<Route
path="/nested-collections"
element={<NestedCollectionsExample />}
/>
<Route
path="/typescript-safety"
element={<WithConverterExample />}
/>
</Routes>
</div>
</div>
</div>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
}

function Navigation() {
const location = useLocation();

const isActive = (path: string) => location.pathname === path;

return (
<nav className="bg-white shadow-sm border-b">
<div className="max-w-6xl mx-auto px-4">
<div className="flex items-center justify-between h-16">
<div className="flex items-center space-x-8">
<Link
to="/"
className={`text-lg font-semibold ${
isActive("/") ? "text-blue-600" : "text-gray-900"
}`}
>
TanStack Query Firebase Examples
</Link>
</div>
<div className="flex items-center space-x-6">
<Link
to="/auth/id-token"
className={`text-sm font-medium ${
isActive("/auth/id-token")
? "text-blue-600"
: "text-gray-500 hover:text-gray-700"
}`}
>
Auth: ID Token
</Link>
<Link
to="/firestore/collection-query"
className={`text-sm font-medium ${
isActive("/firestore/collection-query")
? "text-blue-600"
: "text-gray-500 hover:text-gray-700"
}`}
>
Firestore: Collection Query
</Link>
<Link
to="/nested-collections"
className={`text-sm font-medium ${
isActive("/nested-collections")
? "text-blue-600"
: "text-gray-500 hover:text-gray-700"
}`}
>
Nested Collections
</Link>
<Link
to="/typescript-safety"
className={`text-sm font-medium ${
isActive("/typescript-safety")
? "text-blue-600"
: "text-gray-500 hover:text-gray-700"
}`}
>
TypeScript Safety
</Link>
</div>
</div>
</div>
</nav>
);
}

function Home() {
return (
<div className="text-center">
<h1 className="text-4xl font-bold text-gray-900 mb-6">
TanStack Query Firebase Examples
</h1>
<p className="text-xl text-gray-600 mb-8">
Explore different patterns and use cases for Firebase with TanStack
Query
</p>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
<Link
to="/auth/id-token"
className="p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow"
>
<h2 className="text-lg font-semibold text-gray-900 mb-2">
Auth: ID Token
</h2>
<p className="text-gray-600 text-sm">
Get and refresh Firebase ID tokens with proper caching
</p>
</Link>
<Link
to="/firestore/collection-query"
className="p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow"
>
<h2 className="text-lg font-semibold text-gray-900 mb-2">
Firestore: Collection Query
</h2>
<p className="text-gray-600 text-sm">
Query Firestore collections with filtering and mutations
</p>
</Link>
<Link
to="/nested-collections"
className="p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow"
>
<h2 className="text-lg font-semibold text-gray-900 mb-2">
Nested Collections
</h2>
<p className="text-gray-600 text-sm">
Handle nested Firestore collections with real-time updates
</p>
</Link>
<Link
to="/typescript-safety"
className="p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow"
>
<h2 className="text-lg font-semibold text-gray-900 mb-2">
TypeScript Safety
</h2>
<p className="text-gray-600 text-sm">
Resolve DocumentData vs custom interface type issues
</p>
</Link>
</div>
</div>
);
}

export default App;
Loading