From 2a928303ffa051ae5ef1bfb30e6f8a2a842fc416 Mon Sep 17 00:00:00 2001 From: GravityTwoG Date: Thu, 4 Jul 2024 18:44:08 +0700 Subject: [PATCH] simplify function navigate --- packages/wouter/src/use-hash-location.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/packages/wouter/src/use-hash-location.js b/packages/wouter/src/use-hash-location.js index 35d3171..9838ec5 100644 --- a/packages/wouter/src/use-hash-location.js +++ b/packages/wouter/src/use-hash-location.js @@ -25,22 +25,14 @@ const currentHashLocation = () => "/" + location.hash.replace(/^#?\/?/, ""); export const navigate = (to, { state = null } = {}) => { // calling `replaceState` allows us to set the history // state without creating an extra entry - - let hash = to.replace(/^#?\/?/, ""); - let search = location.search; - - const searchIdx = hash.indexOf("?"); - if (searchIdx !== -1) { - search = hash.slice(searchIdx, hash.length); - hash = hash.slice(0, searchIdx); - } + const [hash, search] = to.replace(/^#?\/?/, "").split("?"); history.replaceState( state, "", // keep the current pathname, but replace query string and hash location.pathname + - search + + (search ? `?${search}` : location.search) + // update location hash, this will cause `hashchange` event to fire // normalise the value before updating, so it's always preceeded with "#/" (location.hash = `#/${hash}`)