Prerequisites
Before creating a custom adapter, ensure:- Your framework provides access to URL search parameters
- Your framework has a way to programmatically update the URL
- You can detect when the URL changes (for synchronization)
Installation
First, install nuqs:Creating a Custom Adapter
Step 1: Import the Adapter Utilities
Import the necessary types and functions fromnuqs/adapters/custom:
Step 2: Define Your Adapter Hook
Create a hook that implements theunstable_AdapterInterface:
Step 3: Create the Adapter Provider
Useunstable_createAdapterProvider to create your adapter component:
Step 4: Use Your Adapter
Wrap your application with your custom adapter:Complete Example
Here’s a complete example of a custom adapter for a hypothetical router:my-custom-adapter.tsx
Advanced: Optimistic Updates
For better UX, you can implement optimistic updates usinguseOptimistic or local state:
Advanced: Rate Limiting
If your router has different rate limiting characteristics, you can specify arateLimitFactor:
Advanced: Auto Queue Reset
Some routers need automatic queue resets on navigation. SetautoResetQueueOnUpdate:
Interface Reference
unstable_AdapterInterface
unstable_AdapterOptions
unstable_UseAdapterHook
renderQueryString
Utility function to render URLSearchParams as a query string:
Examples from Built-in Adapters
React Router Based Adapter Pattern
Many routers share similar patterns. Here’s a simplified version of how nuqs creates React Router adapters:Testing Your Custom Adapter
You can use nuqs’s testing adapter as a reference:Troubleshooting
Type errors with unstable API
The unstable API requires using theunstable_ prefix. Make sure you’re importing correctly:
Infinite re-render loops
Make sure:- Your
searchParamsare properly memoized - Your
updateUrlfunction is wrapped inuseCallback - Dependencies are correctly specified
Search params not syncing
Ensure:- You’re filtering
searchParamsbywatchKeys - Your router’s search params changes trigger re-renders
- The
updateUrlfunction correctly updates your router