Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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

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
133 changes: 133 additions & 0 deletions examples/react/kitchen-sink/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { useState } from "react";
import { Routes, Route, Link, useLocation } from "react-router-dom";
import { IdTokenExample } from "./components/IdTokenExample";
import { CollectionQueryExample } from "./components/CollectionQueryExample";

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 />}
/>
</Routes>
</div>
</div>
</div>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
}

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

const navItems = [
{ path: "/", label: "Home" },
{ path: "/auth/id-token", label: "ID Token Query" },
{ path: "/firestore/collection-query", label: "Collection Query" },
];

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">
<h1 className="text-xl font-bold text-gray-900">
TanStack Query Firebase
</h1>
<div className="flex space-x-4">
{navItems.map((item) => (
<Link
key={item.path}
to={item.path}
className={`px-3 py-2 rounded-md text-sm font-medium transition-colors ${
location.pathname === item.path
? "bg-blue-100 text-blue-700"
: "text-gray-600 hover:text-gray-900 hover:bg-gray-100"
}`}
>
{item.label}
</Link>
))}
</div>
</div>
</div>
</div>
</nav>
);
}

function Home() {
return (
<div className="text-center">
<h1 className="text-4xl font-bold text-gray-900 mb-4">
TanStack Query Firebase Examples
</h1>
<p className="text-xl text-gray-600 mb-8">
Explore different Firebase hooks and patterns with TanStack Query
</p>

<div className="grid md:grid-cols-2 gap-6 max-w-4xl mx-auto">
<div className="bg-white rounded-lg shadow-md p-6">
<h2 className="text-2xl font-bold text-gray-900 mb-4">
Authentication
</h2>
<p className="text-gray-600 mb-4">
Examples of Firebase Authentication hooks including ID token
management.
</p>
<Link
to="/auth/id-token"
className="inline-block bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 transition-colors"
>
View ID Token Example
</Link>
</div>

<div className="bg-white rounded-lg shadow-md p-6">
<h2 className="text-2xl font-bold text-gray-900 mb-4">Firestore</h2>
<p className="text-gray-600 mb-4">
Examples of Firestore hooks for querying collections and documents.
</p>
<Link
to="/firestore/collection-query"
className="inline-block bg-green-600 text-white px-4 py-2 rounded-md hover:bg-green-700 transition-colors"
>
View Collection Query Example
</Link>
</div>
</div>

<div className="mt-12 text-center">
<p className="text-gray-500">
Built with Vite, TanStack Query, React Router, and Firebase
</p>
</div>
</div>
);
}

export default App;
Loading
Loading