-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathRouteAdapter.ts
More file actions
39 lines (35 loc) · 1.11 KB
/
RouteAdapter.ts
File metadata and controls
39 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import React from "react";
import { Location } from "history";
import {
useLocation,
useNavigate,
Location as RouterLocation,
} from "react-router-dom";
// via: https://github.com/pbeshai/use-query-params/issues/196#issuecomment-996893750
interface RouteAdapterProps {
children: React.FunctionComponent<{
history: {
replace(location: Location): void;
push(location: Location): void;
};
location: RouterLocation;
}>;
}
// Via: https://github.com/pbeshai/use-query-params/blob/cd44e7fb3394620f757bfb09ff57b7f296d9a5e6/examples/react-router-6/src/index.js#L36
const RouteAdapter = ({ children }: RouteAdapterProps) => {
const navigate = useNavigate();
const location = useLocation();
const adaptedHistory = React.useMemo(
() => ({
replace(location: Location) {
navigate(location, { replace: true, state: location.state });
},
push(location: Location) {
navigate(location, { replace: false, state: location.state });
},
}),
[navigate]
);
return children && children({ history: adaptedHistory, location });
};
export default RouteAdapter;