forked from molefrog/wouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-location.js
86 lines (73 loc) · 2.88 KB
/
use-location.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { useEffect, useRef, useState, useCallback } from "./react-deps.js";
/**
* History API docs @see https://developer.mozilla.org/en-US/docs/Web/API/History
*/
const eventPopstate = "popstate";
const eventPushState = "pushState";
const eventReplaceState = "replaceState";
export const events = [eventPopstate, eventPushState, eventReplaceState];
export default ({ base = "" } = {}) => {
const [{ path, search }, update] = useState(() => ({
path: currentPathname(base),
search: location.search,
})); // @see https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
const prevHash = useRef(path + search);
useEffect(() => {
// this function checks if the location has been changed since the
// last render and updates the state only when needed.
// unfortunately, we can't rely on `path` value here, since it can be stale,
// that's why we store the last pathname in a ref.
const checkForUpdates = () => {
const pathname = currentPathname(base);
const search = location.search;
const hash = pathname + search;
if (prevHash.current !== hash) {
prevHash.current = hash;
update({ path: pathname, search });
}
};
events.forEach((e) => addEventListener(e, checkForUpdates));
// it's possible that an update has occurred between render and the effect handler,
// so we run additional check on mount to catch these updates. Based on:
// https://gist.github.com/bvaughn/e25397f70e8c65b0ae0d7c90b731b189
checkForUpdates();
return () => events.forEach((e) => removeEventListener(e, checkForUpdates));
}, [base]);
// the 2nd argument of the `useLocation` return value is a function
// that allows to perform a navigation.
//
// the function reference should stay the same between re-renders, so that
// it can be passed down as an element prop without any performance concerns.
const navigate = useCallback(
(to, { replace = false } = {}) =>
history[replace ? eventReplaceState : eventPushState](
null,
"",
// handle nested routers and absolute paths
to[0] === "~" ? to.slice(1) : base + to
),
[base]
);
return [path, navigate];
};
// While History API does have `popstate` event, the only
// proper way to listen to changes via `push/replaceState`
// is to monkey-patch these methods.
//
// See https://stackoverflow.com/a/4585031
if (typeof history !== "undefined") {
for (const type of [eventPushState, eventReplaceState]) {
const original = history[type];
history[type] = function () {
const result = original.apply(this, arguments);
const event = new Event(type);
event.arguments = arguments;
dispatchEvent(event);
return result;
};
}
}
const currentPathname = (base, path = location.pathname) =>
!path.toLowerCase().indexOf(base.toLowerCase())
? path.slice(base.length) || "/"
: "~" + path;