Skip to content

Commit

Permalink
simplify function navigate
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityTwoG committed Jul 4, 2024
1 parent a1aaf39 commit 2a92830
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions packages/wouter/src/use-hash-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down

0 comments on commit 2a92830

Please sign in to comment.