Skip to content

Commit dcb4491

Browse files
add docs and import for useQueryParams hook
1 parent fc48cc2 commit dcb4491

File tree

3 files changed

+73
-14
lines changed

3 files changed

+73
-14
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 }) |

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+
Read and manipulate window.location
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/useQueryParams.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { useEffect } from 'react';
22
import useLocation from './useLocation';
33

4-
// getParams & setParams
5-
64
export default function useQueryParams() {
75
const {
86
replace, search,
@@ -15,18 +13,8 @@ export default function useQueryParams() {
1513
};
1614

1715
const setParams = (params) => {
18-
console.log(params);
19-
const urlSearchParams = new URLSearchParams(params);
20-
replace(`?${urlSearchParams.toString()}`);
21-
22-
console.log(urlSearchParams.toString());
23-
// urlSearchParams.forEach((item) => console.log(item));
24-
// const urlSearchParams = new URLSearchParams();
25-
// Object.entries(params).forEach(([key, value]) => {
26-
// console.log(key, value);
27-
// urlSearchParams.append(key, value);
28-
// });
29-
// replace(`?${urlSearchParams.toString()}`);
16+
const stringfiedUrlSearchParams = new URLSearchParams(params).toString();
17+
replace(`?${stringfiedUrlSearchParams}`);
3018
};
3119

3220
useEffect(() => {
@@ -37,3 +25,34 @@ export default function useQueryParams() {
3725
getParams, setParams,
3826
};
3927
}
28+
29+
// Usage
30+
31+
// function App() {
32+
// const { getParams, setParams } = useQueryParams();
33+
// const params = getParams();
34+
//
35+
// return (
36+
// <div>
37+
// <button
38+
// onClick={() => {
39+
// setParams({ page: 1, order: 'ASC' });
40+
// }}
41+
// >
42+
// Set Params
43+
// </button>
44+
// <button
45+
// onClick={() => {
46+
// setParams({});
47+
// }}
48+
// >
49+
// Clear params
50+
// </button>
51+
// {Object.entries(params).map(([paramKey, paramValue]) => (
52+
// <p>
53+
// {paramKey}: {paramValue}
54+
// </p>
55+
// ))}
56+
// </div>
57+
// );
58+
// }

0 commit comments

Comments
 (0)