Skip to content

Commit

Permalink
Use set method instead of append
Browse files Browse the repository at this point in the history
  • Loading branch information
mutuajames committed Jun 26, 2023
1 parent 2e1c100 commit 738ae19
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
9 changes: 5 additions & 4 deletions packages/react-utils/src/hooks/tests/useSearchParams.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,24 @@ test('useSimpleSearch works correctly', () => {
current.addParam('key', 'value');
current.addParam('key1', 'value1');
current.addParam('key2', 'value2');
expect(current.sParams.toString()).toEqual('key2=value2');
expect(current.sParams.toString()).toEqual('key=value&key1=value1&key2=value2');

expect(history.location).toMatchObject({
hash: '',
key: expect.any(String),
pathname: '/qr',
search: '?key=value?key1=value1?key2=value2',
search: '?key=value?key=value&key1=value1?key=value&key1=value1&key2=value2',
state: undefined,
});

current.removeParam('key1');
expect(current.sParams.toString()).toEqual('key2=value2');
expect(current.sParams.toString()).toEqual('key=value&key2=value2');
expect(history.location).toMatchObject({
hash: '',
key: expect.any(String),
pathname: '/qr',
search: '?key=value?key1=value1?key2=value2?key2=value2',
search:
'?key=value?key=value&key1=value1?key=value&key1=value1&key2=value2?key=value&key2=value2',
state: undefined,
});
});
9 changes: 1 addition & 8 deletions packages/react-utils/src/hooks/useSearchParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ export function useSearchParams() {
return;
}

const keys = sParams.keys();
const keysArray = Array.from(keys);

if (keysArray.length > 2 || keysArray.length < 2) {
sParams.delete(keysArray[keysArray.length - 1]);
}

sParams.append(queryKey, value);
sParams.set(queryKey, value);
const newParams = sParams.toString();
nextUrl = ''.concat(nextUrl, '?').concat(newParams.toString());
history.push(nextUrl);
Expand Down

0 comments on commit 738ae19

Please sign in to comment.