Some cleanup

This commit is contained in:
Max Lynch
2020-12-30 23:01:42 -06:00
parent fc6110e820
commit f19a1d8ac0
8 changed files with 44 additions and 47 deletions
+24 -24
View File
@@ -1,5 +1,29 @@
import { useEffect, useRef, useState, useCallback } from 'react';
// useEffect(() => {
// 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;
};
}
} else {
console.log('No history API');
}
// });
/**
* History API docs @see https://developer.mozilla.org/en-US/docs/Web/API/History
*/
@@ -20,30 +44,6 @@ const useLocation = ({ base = '' } = {}) => {
const [path, update] = useState(() => getCurrentPathname(base)); // @see https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
const prevPath = useRef(path);
useEffect(() => {
// 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;
};
}
} else {
console.log('No history API');
}
});
useEffect(() => {
// this function checks if the location has been changed since the
// last render and updates the state only when needed.