Skip to content

Commit 634556b

Browse files
committed
fix(platform): should useQueryParams only change query
1 parent 2b7e268 commit 634556b

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

packages/platform/src/app/hooks/useQueryParams.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ const KEY = 'query';
1616
export function useQueryParams<T extends {}>(
1717
initParams: Partial<T>
1818
): [T, (newQuery: T | ((draft: T) => void), options?: NavigateOptions) => void] {
19-
const initURLSearchParams = useMemo(
20-
() => new URLSearchParams({ [KEY]: JSURL.stringify(initParams) }),
21-
// eslint-disable-next-line react-hooks/exhaustive-deps
22-
[]
23-
);
24-
const [searchParams, setSearchParams] = useSearchParams(initURLSearchParams);
25-
const queryValues = useMemo<T>(() => JSURL.parse(searchParams.get(KEY)), [searchParams]);
19+
const [searchParams, setSearchParams] = useSearchParams();
20+
// eslint-disable-next-line react-hooks/exhaustive-deps
21+
const queryValues = useMemo<T>(() => (searchParams.get(KEY) ? JSURL.parse(searchParams.get(KEY)) : initParams), [searchParams]);
2622

2723
const setQueryValues = useEventCallback((newValue: T | ((draft: T) => void), options?: NavigateOptions) => {
2824
const newQueryValues = isFunction(newValue) ? produce(queryValues, newValue) : newValue;
29-
setSearchParams(new URLSearchParams({ [KEY]: JSURL.stringify(newQueryValues) }), { replace: true, ...options });
25+
setSearchParams(
26+
(prev) => {
27+
prev.set(KEY, JSURL.stringify(newQueryValues));
28+
return prev;
29+
},
30+
{ replace: true, ...options }
31+
);
3032
});
3133
return [queryValues, setQueryValues];
3234
}

0 commit comments

Comments
 (0)