Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(router): add a more practical hook #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ Open `http://localhost:3000` to view documents about hooks.
| useSearchParamAll | Get a single search param as array |
| useUpdateSearchParams | Get a function to update search params |
| useSearchParamState | Wrap a single search param into a react state |
| useSearchParamArray | Get an array of search param |
| useWebSocket | Create a web socket connecting to specified url |
28 changes: 28 additions & 0 deletions packages/router/docs/useSearchParamsArray.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: useSearchParamArray
nav:
title: Hooks
path: /hook
group:
title: Router
path: /router
order: 8
---

# useSearchParamArray
Like `useSearchParam` but get an array of params you need.

```typescript
function useSearchParamArray(keys: Array<string>): Array<string|null>
```
e.g.
```typescript
import {useSearchParamArray} from '@huse/router';

// query: /test?x=1&y=2
const test = () => {
const [x, y, z] = useSearchParamArray(['x','y']);
// 1 2 null
console.log(x, y, z);
};
```
18 changes: 18 additions & 0 deletions packages/router/src/__tests__/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useSearchParamAll,
useUpdateSearchParams,
useSearchParamState,
useSearchParamArray,
} from '../search';

const wrapper = historyOrURL => {
Expand Down Expand Up @@ -155,3 +156,20 @@ describe('useSearchParamState', () => {
expect(result.current[0]).toBe('2');
});
});

describe('useSearchParamArray', () => {
test('single value', () => {
const {result} = renderHook(() => useSearchParamArray('x'), {wrapper: wrapper('/foo?x=1')});
expect(result.current).toEqual(['1']);
});

test('multiple values', () => {
const {result} = renderHook(() => useSearchParamArray(['x','y']), {wrapper: wrapper('/foo?x=1&y=2')});
expect(result.current).toEqual(['1', '2']);
});

test('no value', () => {
const {result} = renderHook(() => useSearchParamArray(['y']), {wrapper: wrapper('/foo?x=1')});
expect(result.current).toEqual([null]);
});
});
9 changes: 9 additions & 0 deletions packages/router/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,12 @@ export function useSearchParamState(key: string, options: NavigateOptions = {}):

return [value, setValue];
}

export function useSearchParamArray(keys: Array<string>): Array<string|null> {
const result: Array<string|null> = [];
const [params] = useSearchParams();
for(const key of keys){
result.push(params.get(key));
}
return result;
}