1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/frontend/src/hooks/useQueryStringState.ts
olav d8143c6ff4 chore: update react-router to v6 (#946)
* refactor: fix child selector warnings

* refactor: update react-router-dom

* refactor: use BrowserRouter as in react-router docs

* refactor: replace Redirect with Navigate

* refactor: replace Switch with Routes

* refactor: replace useHistory with useNavigate

* refactor: replace useParams types with useRequiredPathParam

* refactor: replace NavLink activeStyle with callback

* refactor: fix matchPath arg order

* refactor: Remove unused link state

* refactor: delete broken snapshot test

* refactor: render 404 page without redirect

* refactor: normalize path parameter names

* refactor: fix Route component usage
2022-05-05 13:42:18 +02:00

26 lines
785 B
TypeScript

import { useCallback, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
// Store a value in the query string. Call setState to update the query string.
export const useQueryStringState = (
key: string
): [string | undefined, (value: string) => void] => {
const { search } = window.location;
const navigate = useNavigate();
const params = useMemo(() => {
return new URLSearchParams(search);
}, [search]);
const setState = useCallback(
(value: string) => {
const next = new URLSearchParams(search);
next.set(key, value);
navigate({ search: next.toString() }, { replace: true });
},
[key, search, navigate]
);
return [params.get(key) || undefined, setState];
};