Skip to content

Commit

Permalink
[v6] Add <HistoryRouter> for standalone history objects (#7586)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Dorr <git@timdorr.com>
  • Loading branch information
salvoravida and timdorr committed Dec 2, 2021
1 parent e847506 commit 8a04a65
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/react-router-dom/index.tsx
@@ -1,5 +1,5 @@
import * as React from "react";
import type { BrowserHistory, HashHistory } from "history";
import type { BrowserHistory, HashHistory, History } from "history";
import { createBrowserHistory, createHashHistory, createPath } from "history";
import {
MemoryRouter,
Expand Down Expand Up @@ -194,6 +194,39 @@ export function HashRouter({ basename, children, window }: HashRouterProps) {
);
}

export interface HistoryRouterProps {
basename?: string;
children?: React.ReactNode;
history: History;
}

export function HistoryRouter({
basename,
children,
history
}: HistoryRouterProps) {
const [state, setState] = React.useState({
action: history.action,
location: history.location
});

React.useLayoutEffect(() => history.listen(setState), [history]);

return (
<Router
basename={basename}
children={children}
location={state.location}
navigationType={state.action}
navigator={history}
/>
);
}

if (__DEV__) {
HistoryRouter.displayName = "HistoryRouter";
}

function isModifiedEvent(event: React.MouseEvent) {
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
}
Expand Down

0 comments on commit 8a04a65

Please sign in to comment.