-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from Arthurgallina1/useQueryParams
Add useQueryParams hook
- Loading branch information
Showing
5 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import useQueryParams from '../src/useQueryParams'; | ||
|
||
describe('useQueryParams', () => { | ||
it('sets query params correctly', () => { | ||
const newParams = { page: '1', order: 'ASC' }; | ||
const { result } = renderHook(() => useQueryParams()); | ||
|
||
act(() => { | ||
result.current.setParams(newParams); | ||
}); | ||
const params = result.current.getParams(); | ||
expect(params).toStrictEqual(newParams); | ||
}); | ||
|
||
it('clears query params', () => { | ||
const { result } = renderHook(() => useQueryParams()); | ||
|
||
act(() => { | ||
result.current.setParams(); | ||
}); | ||
const params = result.current.getParams(); | ||
expect(params).toStrictEqual({}); | ||
}); | ||
|
||
it('read query params correctly', () => { | ||
delete window.location; | ||
window.location = new URL('https://test.com/testing?param1=test&testing=true'); | ||
|
||
const { result } = renderHook(() => useQueryParams()); | ||
let params; | ||
act(() => { | ||
params = result.current.getParams(); | ||
}); | ||
expect(params).toStrictEqual({ param1: 'test', testing: 'true' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 📍 `useQueryParams` | ||
|
||
Gets and sets query params | ||
|
||
## Usage | ||
|
||
```js | ||
import { useQueryParams } from 'react-recipes'; | ||
|
||
function App() { | ||
const { getParams, setParams } = useQueryParams(); | ||
|
||
const params = getParams(); | ||
|
||
return ( | ||
<div> | ||
<button | ||
onClick={() => { | ||
setParams({ page: 1, order: 'ASC' }); | ||
}} | ||
> | ||
Set Params | ||
</button> | ||
<button | ||
onClick={() => { | ||
setParams({}); | ||
}} | ||
> | ||
Clear params | ||
</button> | ||
{Object.entries(params).map(([paramKey, paramValue]) => ( | ||
<p> | ||
{paramKey}: {paramValue} | ||
</p> | ||
))} | ||
</div> | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import useLocation from './useLocation'; | ||
|
||
export default function useQueryParams() { | ||
const { | ||
replace, search, | ||
} = useLocation(); | ||
|
||
const getParams = () => { | ||
const urlSearchParams = new URLSearchParams(search); | ||
const params = Object.fromEntries(urlSearchParams.entries()); | ||
return params; | ||
}; | ||
|
||
const setParams = (params) => { | ||
const stringfiedUrlSearchParams = new URLSearchParams(params).toString(); | ||
replace(`?${stringfiedUrlSearchParams}`); | ||
}; | ||
|
||
return { | ||
getParams, setParams, | ||
}; | ||
} | ||
|
||
// Usage | ||
|
||
// function App() { | ||
// const { getParams, setParams } = useQueryParams(); | ||
// const params = getParams(); | ||
// | ||
// return ( | ||
// <div> | ||
// <button | ||
// onClick={() => { | ||
// setParams({ page: 1, order: 'ASC' }); | ||
// }} | ||
// > | ||
// Set Params | ||
// </button> | ||
// <button | ||
// onClick={() => { | ||
// setParams({}); | ||
// }} | ||
// > | ||
// Clear params | ||
// </button> | ||
// {Object.entries(params).map(([paramKey, paramValue]) => ( | ||
// <p> | ||
// {paramKey}: {paramValue} | ||
// </p> | ||
// ))} | ||
// </div> | ||
// ); | ||
// } |