# Using Hooks
This guide explains how to use React hooks for data fetching and state management.
## Hook Types
The app has two types of hooks:
- **Query Hooks** - Fetch data (GET requests)
- **Mutation Hooks** - Modify data (POST, PUT, DELETE requests)
All hooks use **TanStack React Query** for caching and state management.
## Query Hooks (Read Operations)
Query hooks fetch data and cache it automatically.
### Using a Query Hook
```typescript
'use client';
import { useProfileQuery } from '@/lib/hooks/queries/use-profile-query';
export function UserProfile() {
const { data, isLoading, error } = useProfileQuery();
if (isLoading) return
Loading...
;
if (error) return
Error: {error.message}
;
return
Hello, {data.name}
;
}
```
### Query Hook Lifecycle
```mermaid
graph LR
A[Component mounts] --> B[Hook called]
B --> C{Data in cache?}
C -->|Yes| D[Return cached data]
C -->|No| E[Fetch from API]
D --> F[Background refetch if stale]
E --> G[Cache result]
F --> G
G --> H[Component rerenders]
```
### Available Query Hooks
**File**: `lib/hooks/queries/`
- **`useProfileQuery()`** - Current user profile
- **`useSubscriptionQuery()`** - Subscription status
- **`useProductsQuery()`** - Product list
- **`useMembersQuery()`** - Team members
- **`useDocumentsQuery()`** - Document list
- **`useSessionsQuery()`** - Audit sessions
- **`useInvoicesQuery()`** - Invoice list
- **`useVendorsQuery()`** - Vendor list
## Mutation Hooks (Write Operations)
Mutation hooks modify data on the server.
### Using a Mutation Hook
```typescript
'use client';
import { useUpdateProfile } from '@/lib/hooks/mutations/use-update-profile';
export function ProfileForm() {
const { mutate, isPending, error } = useUpdateProfile();
const handleSubmit = () => {
mutate({
name: 'New Name',
email: 'new@example.com'
});
};
return (
{error &&
Error: {error.message}
}
);
}
```
### Mutation Flow
```mermaid
graph TD
A[User clicks button] --> B[Call mutate]
B --> C[Send API request]
C --> D{Success?}
D -->|Yes| E[Invalidate queries]
D -->|No| F[Show error]
E --> G[Refetch affected data]
G --> H[Update UI]
F --> H
```
### Available Mutation Hooks
**File**: `lib/hooks/mutations/`
- **`useUpdateProfile()`** - Update user profile
- **`useInviteMember()`** - Invite team member
- **`useRemoveMember()`** - Remove team member
- **`useResendInvitation()`** - Resend invitation
- **`useUploadDocument()`** - Upload document
- **`useDeleteDocument()`** - Delete document
- **`useChat()`** - AI chat
- **`useCreateInvoice()`** - Create invoice
- **`useDeleteInvoice()`** - Delete invoice
## Permission Hook
The `usePermissions()` hook provides auth state and permission checks.
**File**: `lib/hooks/use-permissions.ts`
```typescript
'use client';
import { usePermissions } from '@/lib/hooks/use-permissions';
export function PermissionGate() {
const {
profile, // User profile
roles, // User roles array
permissions, // Permissions array
hasPermission, // Check single permission
hasAnyPermission, // Check if has any
hasAllPermissions, // Check if has all
hasRole, // Check single role
hasAnyRole, // Check if has any role
hasAllRoles, // Check if has all roles
isAuthenticated, // Boolean
isInitialized, // Boolean (Stytch ready)
updateAuthState, // Manual update function
} = usePermissions();
if (!isAuthenticated) {
return