Skip to content

Commit

Permalink
add HistoryRouter for support already created history obj
Browse files Browse the repository at this point in the history
  • Loading branch information
salvoravida committed Nov 13, 2021
1 parent 1281d1c commit cc31f39
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 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,37 @@ 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 cc31f39

Please sign in to comment.