busser
busser copied to clipboard
[WIP-FEATURE-REQUEST | v1.x]: useHistoryRouter() hook: Implement for both NextJS and ReactJS
Feature request
Description / Observed Behavior
There is need to implement a
useHistoryRouter()
hook that works with both NextJS and ReactJS.
Additional Context
import {
useHistory
} from "react-router-dom";
import {
useNavigate
} from "react-router-dom-v5-compat";
import { useRouter } from "next/router";
const useHistoryRouter = () => {
const isReactRouterDOMRouting = typeof useHistory === "function";
const useBrowserRouter = isReactRouterDOMRouting ? useHistory : useRouter;
const router = useBrowserRouter();
const navigator = typeof useNavigate === "function" ? useNavigate() : null;
return {
navigateTo (urlPath) {
if (navigator !== null) {
return navigate(urlPath)
} else {
return router.push(urlPath);
}
},
get pathname () {
if (Boolean(router.location)) {
return router.location.pathname;
} else {
return router.pathname;
}
},
get state () {
if (Boolean(router.location)) {
return router.location.state;
} else {
return null;
}
},
get search () {
if (Boolean(router.location)) {
const searchParams = new URLSearchParams(
router.location.search
);
return Object.fromEntries(
searchParams.entries()
);
} else {
return router.query;
}
},
navigateBack () {
if (typeof router.back === "function") {
router.back();
} else {
router.goBack();
}
},
reload () {
if (typeof router.reload === "function") {
return router.reload();
} else {
return router.location.reload()
}
}
};
};
Request for busser version.
v0.1.2