Skip to content

Commit 11aad9d

Browse files
salvoravidatimdorr
andcommitted
[v6] Add <HistoryRouter> for standalone history objects (#7586)
Co-authored-by: Tim Dorr <[email protected]>
1 parent 6e6a687 commit 11aad9d

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

packages/react-router-dom/index.tsx

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import type { BrowserHistory, HashHistory } from "history";
2+
import type { BrowserHistory, HashHistory, History } from "history";
33
import { createBrowserHistory, createHashHistory, createPath } from "history";
44
import {
55
MemoryRouter,
@@ -194,6 +194,39 @@ export function HashRouter({ basename, children, window }: HashRouterProps) {
194194
);
195195
}
196196

197+
export interface HistoryRouterProps {
198+
basename?: string;
199+
children?: React.ReactNode;
200+
history: History;
201+
}
202+
203+
export function HistoryRouter({
204+
basename,
205+
children,
206+
history
207+
}: HistoryRouterProps) {
208+
const [state, setState] = React.useState({
209+
action: history.action,
210+
location: history.location
211+
});
212+
213+
React.useLayoutEffect(() => history.listen(setState), [history]);
214+
215+
return (
216+
<Router
217+
basename={basename}
218+
children={children}
219+
location={state.location}
220+
navigationType={state.action}
221+
navigator={history}
222+
/>
223+
);
224+
}
225+
226+
if (__DEV__) {
227+
HistoryRouter.displayName = "HistoryRouter";
228+
}
229+
197230
function isModifiedEvent(event: React.MouseEvent) {
198231
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
199232
}

0 commit comments

Comments
 (0)