File size: 1,034 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# `useSearchParam`
React sensor hook that tracks browser's location search param.
## Usage
```jsx
import {useSearchParam} from 'react-use';
const Demo = () => {
const edit = useSearchParam('edit');
return (
<div>
<div>edit: {edit || '🤷♂️'}</div>
<div>
<button onClick={() => history.pushState({}, '', location.pathname + '?edit=123')}>Edit post 123 (?edit=123)</button>
</div>
<div>
<button onClick={() => history.pushState({}, '', location.pathname + '?edit=999')}>Edit post 999 (?edit=999)</button>
</div>
<div>
<button onClick={() => history.pushState({}, '', location.pathname)}>Close modal</button>
</div>
</div>
);
};
```
## Caveats/Gotchas
When using a hash router, like `react-router`'s [`<HashRouter>`](https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/HashRouter.md), this hook won't be able to read the search parameters as they are considered part of the hash of the URL by browsers.
|