Installation
All React components automatically include the client setup files. When you install any component, these files are added to your project:lib/convex/client.ts- Convex client instancelib/convex/provider.tsx- React provider component
Client Configuration
The client is configured to connect to your Convex deployment using environment variables.lib/convex/client.ts
For Vite-based React apps, environment variables must be prefixed with
VITE_ to be accessible in the browser.Provider Setup
The provider wraps your application and enables Convex queries, mutations, and authentication.lib/convex/provider.tsx
Usage
Wrap your application root with theConvexClientProvider:
Environment Variables
Create a.env file in your project root:
.env
Run
npx convex dev to start the Convex development server. The deployment URL will be displayed in the terminal.Copy the URL (e.g.,
https://happy-animal-123.convex.cloud) and add it to your .env file as VITE_CONVEX_URL.Features
Real-time Queries
Automatically subscribes to database changes and re-renders components when data updates.
Authentication
Integrates Convex Auth for user authentication with password and OAuth providers.
Type Safety
Full TypeScript support with generated types from your Convex schema.
Optimistic Updates
Update UI instantly while mutations process in the background.
Using Convex Hooks
Once the provider is set up, you can use Convex hooks anywhere in your app:API Reference
ConvexClientProvider
The main provider component that wraps your application.Your application components
convex
The Convex client instance. You can import this directly if you need to access the client imperatively:Troubleshooting
Error: VITE_CONVEX_URL is not defined
Error: VITE_CONVEX_URL is not defined
Make sure you’ve created a
.env file with VITE_CONVEX_URL and restarted your dev server. Vite only loads environment variables on startup.Queries return undefined
Queries return undefined
This is normal while the initial connection is being established. Check the connection status with
useConvex() hook or add loading states to your components.Type errors in api imports
Type errors in api imports
Run
npx convex dev to generate TypeScript types from your Convex functions. Make sure the convex/_generated directory is created.Next Steps
Password Auth
Add email/password authentication to your app
Social Auth
Enable GitHub and Google sign-in
Realtime Chat
Build a real-time chat component
File Upload
Add file upload with Convex storage