Skip to content

Commit

Permalink
refactor(hash-location): use navigate from use-browser-location
Browse files Browse the repository at this point in the history
  • Loading branch information
junwen-k committed Nov 30, 2023
1 parent daec7b0 commit 3d4b2b9
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions packages/wouter/src/use-hash-location.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { navigate as browserNavigate } from "./use-browser-location.js";
import { useSyncExternalStore } from "./react-deps.js";

// array of callback subscribed to hash updates
Expand All @@ -23,17 +24,18 @@ const subscribeToHashUpdates = (callback) => {
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
history.replaceState(
state,
"",
// keep the current pathname, current query string, but replace the hash
browserNavigate(
location.pathname +
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 = `#/${to.replace(/^#?\/?/, "")}`)
(location.hash = `#/${to.replace(/^#?\/?/, "")}`),
{
// calling `replaceState` allows us to set the history
// state without creating an extra entry
replace: true,
state,
}
);
};

Expand Down

0 comments on commit 3d4b2b9

Please sign in to comment.