Commit 14de2928 authored by Max Alekseenko's avatar Max Alekseenko

keep hash when updating query

parent 85c94179
import type { NextRouter } from 'next/router'; import type { NextRouter } from 'next/router';
export default function removeQueryParam(router: NextRouter, param: string) { export default function removeQueryParam(router: NextRouter, param: string) {
const { pathname, query } = router; const { pathname, query, asPath } = router;
const newQuery = { ...query }; const newQuery = { ...query };
delete newQuery[param]; delete newQuery[param];
router.replace({ pathname, query: newQuery }, undefined, { shallow: true });
const hashIndex = asPath.indexOf('#');
const hash = hashIndex !== -1 ? asPath.substring(hashIndex) : '';
router.replace({ pathname, query: newQuery, hash }, undefined, { shallow: true });
} }
import type { NextRouter } from 'next/router'; import type { NextRouter } from 'next/router';
export default function updateQueryParam(router: NextRouter, param: string, newValue: string) { export default function updateQueryParam(router: NextRouter, param: string, newValue: string) {
const { pathname, query } = router; const { pathname, query, asPath } = router;
const newQuery = { ...query }; const newQuery = { ...query };
newQuery[param] = newValue; newQuery[param] = newValue;
router.replace({ pathname, query: newQuery }, undefined, { shallow: true });
const hashIndex = asPath.indexOf('#');
const hash = hashIndex !== -1 ? asPath.substring(hashIndex) : '';
router.replace({ pathname, query: newQuery, hash }, undefined, { shallow: true });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment