Skip to content

Commit

Permalink
fix changing parameter to an empty array (should clear the parameter)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaddix committed Aug 24, 2022
1 parent 7233616 commit 98fa3eb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 7 additions & 0 deletions example/ArrayParamPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export const ArrayParamPage = () => {
>
Change ids to 3,4
</button>
<button
onClick={() => {
setQueryParams({ ids: [] });
}}
>
Change ids to []
</button>
</div>
<div>
Id: {queryParams.id}
Expand Down
12 changes: 8 additions & 4 deletions src/useQueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ export const useQueryParams = <QPCMap extends QueryParamConfigMap>(
Object.keys(values).forEach((key) => {
const keyValue = (encoded as any)[key];
if (Array.isArray(keyValue)) {
keyValue.forEach((v, index) => {
if (index === 0) searchParamsRef.current.set(key, v);
else searchParamsRef.current.append(key, v);
});
if (keyValue.length > 0) {
keyValue.forEach((v, index) => {
if (index === 0) searchParamsRef.current.set(key, v);
else searchParamsRef.current.append(key, v);
});
} else {
searchParamsRef.current.delete(key);
}
} else {
if (keyValue === undefined || keyValue === null) {
searchParamsRef.current.delete(key);
Expand Down

0 comments on commit 98fa3eb

Please sign in to comment.