Skip to content

Commit 16842c2

Browse files
refactor(common): convert routing to TypeScript
1 parent 8cf5e9f commit 16842c2

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as withRouterAndRef } from './withRouterAndRef';
File renamed without changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as React from 'react';
2+
import { Route } from 'react-router-dom';
3+
4+
// Basically a workaround for the fact that react-router's withRouter cannot forward ref's through
5+
// functional components. Use this instead to gain the benefits of withRouter but also ref forwarding
6+
export default function withRouterAndRef(Wrapped: React.ComponentType<Record<string, unknown>>) {
7+
const WithRouterAndRef = React.forwardRef((props: Record<string, unknown>, ref: React.Ref<unknown>) => (
8+
<Route>{routeProps => <Wrapped ref={ref} {...routeProps} {...props} />}</Route>
9+
));
10+
const name = Wrapped.displayName || Wrapped.name || 'Component';
11+
WithRouterAndRef.displayName = `withRouterAndRef(${name})`;
12+
return WithRouterAndRef;
13+
}

0 commit comments

Comments
 (0)