Skip to content

Commit 741d934

Browse files
authored
Merge pull request #58 from Arthurgallina1/useQueryParams
Add useQueryParams hook
2 parents 733973b + 0f279c0 commit 741d934

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ yarn add react-recipes
5353
| 🥒 [`useOnlineStatus`](./docs/useOnlineStatus.md) | onlineStatus | - |
5454
| 🍿 [`usePrevious`](./docs/usePrevious.md) | previous | (value) |
5555
| 🖨 [`usePrint`](./docs/usePrint.md) | { ref, handlePrint } | (style = {}) |
56+
| :question: [`useQueryParams`](./docs/useQueryParams.md) | { getParams, setParams } | - |
5657
| 🍣 [`useScript`](./docs/useScript.md) | [loaded, error] | (src) |
5758
| 🍖 [`useSpeechRecognition`](./docs/useSpeechRecognition.md) | { supported, listen, listening, stop } | ({ onEnd, onResult, onError }) |
5859
| 🍗 [`useSpeechSynthesis`](./docs/useSpeechSynthesis.md) | { supported, speak, speaking, cancel, voices, pause, resume } | ({ onEnd, onResult, onError, onBoundary, onPause, onResume }) |

__tests__/useQueryParams.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { act, renderHook } from '@testing-library/react-hooks';
2+
import useQueryParams from '../src/useQueryParams';
3+
4+
describe('useQueryParams', () => {
5+
it('sets query params correctly', () => {
6+
const newParams = { page: '1', order: 'ASC' };
7+
const { result } = renderHook(() => useQueryParams());
8+
9+
act(() => {
10+
result.current.setParams(newParams);
11+
});
12+
const params = result.current.getParams();
13+
expect(params).toStrictEqual(newParams);
14+
});
15+
16+
it('clears query params', () => {
17+
const { result } = renderHook(() => useQueryParams());
18+
19+
act(() => {
20+
result.current.setParams();
21+
});
22+
const params = result.current.getParams();
23+
expect(params).toStrictEqual({});
24+
});
25+
26+
it('read query params correctly', () => {
27+
delete window.location;
28+
window.location = new URL('https://test.com/testing?param1=test&testing=true');
29+
30+
const { result } = renderHook(() => useQueryParams());
31+
let params;
32+
act(() => {
33+
params = result.current.getParams();
34+
});
35+
expect(params).toStrictEqual({ param1: 'test', testing: 'true' });
36+
});
37+
});

docs/useQueryParams.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 📍 `useQueryParams`
2+
3+
Gets and sets query params
4+
5+
## Usage
6+
7+
```js
8+
import { useQueryParams } from 'react-recipes';
9+
10+
function App() {
11+
const { getParams, setParams } = useQueryParams();
12+
13+
const params = getParams();
14+
15+
return (
16+
<div>
17+
<button
18+
onClick={() => {
19+
setParams({ page: 1, order: 'ASC' });
20+
}}
21+
>
22+
Set Params
23+
</button>
24+
<button
25+
onClick={() => {
26+
setParams({});
27+
}}
28+
>
29+
Clear params
30+
</button>
31+
{Object.entries(params).map(([paramKey, paramValue]) => (
32+
<p>
33+
{paramKey}: {paramValue}
34+
</p>
35+
))}
36+
</div>
37+
);
38+
}
39+
```

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export { default as useOnClickOutside } from './useOnClickOutside';
2323
export { default as useOnlineStatus } from './useOnlineStatus';
2424
export { default as usePrevious } from './usePrevious';
2525
export { default as usePrint } from './usePrint';
26+
export { default as useQueryParams } from './useQueryParams';
2627
export { default as useScript } from './useScript';
2728
export { default as useSpeechRecognition } from './useSpeechRecognition';
2829
export { default as useSpeechSynthesis } from './useSpeechSynthesis';

src/useQueryParams.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import useLocation from './useLocation';
2+
3+
export default function useQueryParams() {
4+
const {
5+
replace, search,
6+
} = useLocation();
7+
8+
const getParams = () => {
9+
const urlSearchParams = new URLSearchParams(search);
10+
const params = Object.fromEntries(urlSearchParams.entries());
11+
return params;
12+
};
13+
14+
const setParams = (params) => {
15+
const stringfiedUrlSearchParams = new URLSearchParams(params).toString();
16+
replace(`?${stringfiedUrlSearchParams}`);
17+
};
18+
19+
return {
20+
getParams, setParams,
21+
};
22+
}
23+
24+
// Usage
25+
26+
// function App() {
27+
// const { getParams, setParams } = useQueryParams();
28+
// const params = getParams();
29+
//
30+
// return (
31+
// <div>
32+
// <button
33+
// onClick={() => {
34+
// setParams({ page: 1, order: 'ASC' });
35+
// }}
36+
// >
37+
// Set Params
38+
// </button>
39+
// <button
40+
// onClick={() => {
41+
// setParams({});
42+
// }}
43+
// >
44+
// Clear params
45+
// </button>
46+
// {Object.entries(params).map(([paramKey, paramValue]) => (
47+
// <p>
48+
// {paramKey}: {paramValue}
49+
// </p>
50+
// ))}
51+
// </div>
52+
// );
53+
// }

0 commit comments

Comments
 (0)