-
Hi everyone |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
Hi Amir, thanks for opening this discussion. Do you have a link to your implementation with react-router? One thing I'd love to be able to do eventually, is for the A non-exhaustive list of such frameworks would be:
One way to do this could be to provide the implementation (interfaces with each framework/router's APIs) via Context, but this introduces an additional step to integrate nuqs, and nuqs-aware components. Ideally, an automatic way that doesn't break locality of behaviour would be best: at import time, the correct implementation is selected and loaded. This shifts the problem/burden onto bundlers, which would add another additional level of complexity. Another issue is the options: some of them make sense in the context of a full-stack app with a server ready to receive updated search params in a request, and render an updated view. But most traditional React apps are SPAs without any form of SSR or BFF. This raises the question of what to do with the |
Beta Was this translation helpful? Give feedback.
-
For people visiting this forum for an inertia adapter, here is some quick hacky solution I made, it seems to work fine. import { router } from '@inertiajs/core';
import { startTransition, useCallback, useEffect, useState } from 'react';
import mitt, { Emitter } from 'mitt';
import {
renderQueryString,
unstable_AdapterInterface,
unstable_AdapterOptions,
unstable_createAdapterProvider,
} from 'nuqs/adapters/custom';
export type SearchParamsSyncEmitter = Emitter<{ update: URLSearchParams }>;
const emitter: SearchParamsSyncEmitter = mitt();
function useNuqsInertiaAdapter(): unstable_AdapterInterface {
const [searchParams, setSearchParams] = useState(() => {
if (typeof location === 'undefined') {
return new URLSearchParams();
}
return new URLSearchParams(location.search);
});
const updateUrl = useCallback(
(search: URLSearchParams, options: Required<unstable_AdapterOptions>) => {
startTransition(() => {
const url = new URL(location.href);
url.search = renderQueryString(search);
emitter.emit('update', search);
if (!options.shallow) {
const visitOptions = {
preserveScroll: !options.scroll,
preserveState: true,
replace: options.history === 'replace',
};
router.visit(url.pathname + url.search, visitOptions);
}
});
},
[],
);
useEffect(() => {
function onPopState() {
setSearchParams(new URLSearchParams(location.search));
}
function onEmitterUpdate(search: URLSearchParams) {
setSearchParams(search);
}
emitter.on('update', onEmitterUpdate);
window.addEventListener('popstate', onPopState);
return () => {
emitter.off('update', onEmitterUpdate);
window.removeEventListener('popstate', onPopState);
};
}, []);
return {
searchParams,
updateUrl,
rateLimitFactor: 1,
};
}
export const NuqsAdapter = unstable_createAdapterProvider(
useNuqsInertiaAdapter,
);
// /**
// I haven't tested this
// export function enableHistorySync() {
// patchHistory(emitter, 'inertia');
// } |
Beta Was this translation helpful? Give feedback.
I have opened a draft PR implementing adapters in #644. You can try a preview here (here be dragons):
You'll need to wrap your application in a
NuqsAdapter
, as such:Vanilla React (eg: with Vite):
Next.js, app router: