Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(use-browser-location): inline location snapshot function #422

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions packages/wouter/src/use-browser-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@ const subscribeToLocationUpdates = (callback) => {
export const useLocationProperty = (fn, ssrFn) =>
useSyncExternalStore(subscribeToLocationUpdates, fn, ssrFn);

const currentSearch = () => location.search;

export const useSearch = ({ ssrSearch = "" } = {}) =>
useLocationProperty(currentSearch, () => ssrSearch);

const currentPathname = () => location.pathname;
useLocationProperty(
() => location.search,
() => ssrSearch
);

export const usePathname = ({ ssrPath } = {}) =>
useLocationProperty(
currentPathname,
ssrPath ? () => ssrPath : currentPathname
() => location.pathname,
ssrPath ? () => ssrPath : () => location.pathname
);

const currentHistoryState = () => history.state;
export const useHistoryState = () =>
useLocationProperty(currentHistoryState, () => null);
useLocationProperty(
() => history.state,
() => null
);

export const navigate = (to, { replace = false, state = null } = {}) =>
history[replace ? eventReplaceState : eventPushState](state, "", to);
Expand Down