Skip to content

Commit cc31f39

Browse files
committed
add HistoryRouter for support already created history obj
1 parent 1281d1c commit cc31f39

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

packages/react-router-dom/index.tsx

Lines changed: 32 additions & 1 deletion
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,37 @@ 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({ basename, children, history }: HistoryRouterProps) {
204+
const [state, setState] = React.useState({
205+
action: history.action,
206+
location: history.location,
207+
});
208+
209+
React.useLayoutEffect(() => history.listen(setState), [history]);
210+
211+
return (
212+
<Router
213+
basename={basename}
214+
children={children}
215+
location={state.location}
216+
navigationType={state.action}
217+
navigator={history}
218+
/>
219+
);
220+
}
221+
222+
223+
224+
if (__DEV__) {
225+
HistoryRouter.displayName = 'HistoryRouter';
226+
}
227+
197228
function isModifiedEvent(event: React.MouseEvent) {
198229
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
199230
}

0 commit comments

Comments
 (0)