React Providers
Drift Common provides React context providers that set up the necessary infrastructure for Drift applications, including wallet integration, Drift client initialization, and application-wide state management.Available Providers
- DriftProvider - Main provider that orchestrates all Drift functionality
- DriftWalletProvider - Wallet adapter integration with custom autoconnect logic
DriftProvider
The main provider component that sets up the complete Drift application infrastructure. This provider should wrap your entire application.Import
Props
Basic Usage
Complete Example
Custom Breakpoints
Disabling Features
What DriftProvider Does
The DriftProvider sets up several features automatically:- Wallet Integration - Configures wallet adapters and manages connections
- Drift Client - Initializes and manages the DriftClient connection
- State Management - Sets up Zustand stores for application state
- Screen Size Tracking - Monitors window size based on breakpoints
- SOL Balance - Subscribes to wallet SOL balance updates
- Idle Detection - Adjusts polling rates when user is inactive
- Emulation Mode - Enables transaction simulation mode
- Geoblocking - Checks user location restrictions
DriftWalletProvider
A wrapper around Solana’s WalletProvider with Drift-specific wallet logic, including custom autoconnect behavior and wallet filtering.Import
Props
Basic Usage
Features
Custom Autoconnect Logic
The DriftWalletProvider implements custom autoconnect logic that:- Waits for a configurable delay before attempting to connect
- Only attempts to connect if the wallet was previously connected
- Stores the preferred wallet in localStorage
- Handles connection and disconnection events
Mobile Wallet Adapter Filtering
On devices where the Mobile Wallet Adapter is available, the provider automatically filters out other wallets to avoid confusion and bugs:Wallet State Persistence
The provider automatically manages wallet state in localStorage:Advanced Usage
Accessing Wallet Context
Using with DriftProvider
Typically, you don’t need to use DriftWalletProvider directly as it’s included in DriftProvider:Best Practices
Provider Ordering
When using DriftProvider, it handles the correct provider ordering automatically. If you’re using providers separately, ensure proper nesting:Wallet Memoization
Memoize wallet adapters to prevent unnecessary re-renders:Environment Configuration
Initialize the Drift store before rendering your app:Error Boundaries
Wrap your app in error boundaries to handle provider initialization errors:Related
- React Stores - State management stores used by providers
- Wallet Hook - Hook for accessing wallet state
- Drift Client Hook - Hook for accessing Drift client
- Setup Guide - Complete setup guide for React package