Skip to content

Instantly share code, notes, and snippets.

@audiolion
Created November 22, 2019 15:24
Show Gist options
  • Save audiolion/3858d016a6f06d9ca19486d2df007f10 to your computer and use it in GitHub Desktop.
Save audiolion/3858d016a6f06d9ca19486d2df007f10 to your computer and use it in GitHub Desktop.
import qs from 'query-string';
import { useHistory, useLocation } from 'react-router';
function useSynchronizeQueryParams({ topic }) {
const { search, pathname } = useLocation();
const history = useHistory();
const params = qs.parse(search);
React.useEffect(() => {
if (!params.topic && topic) {
const nextUrl = `${pathname}?${qs.stringify({
...params,
topic,
})}`;
history.replace(nextUrl);
}
}, [topic, params, pathname, history]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment