Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/elements/common/routing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as withRouterAndRef } from './withRouterAndRef';
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Route } from 'react-router-dom';
// Basically a workaround for the fact that react-router's withRouter cannot forward ref's through
// functional components. Use this instead to gain the benefits of withRouter but also ref forwarding
export default function withRouterAndRef(Wrapped: React.ComponentType<any>) {
const WithRouterAndRef = React.forwardRef<Object, React.Ref<any>>((props, ref) => (
const WithRouterAndRef = React.forwardRef((props: any, ref: React.Ref<any>) => (
<Route>{routeProps => <Wrapped ref={ref} {...routeProps} {...props} />}</Route>
));
const name = Wrapped.displayName || Wrapped.name || 'Component';
Expand Down
13 changes: 13 additions & 0 deletions src/elements/common/routing/withRouterAndRef.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as React from 'react';
import { Route } from 'react-router-dom';

// Basically a workaround for the fact that react-router's withRouter cannot forward ref's through
// functional components. Use this instead to gain the benefits of withRouter but also ref forwarding
export default function withRouterAndRef(Wrapped: React.ComponentType<Record<string, unknown>>) {
const WithRouterAndRef = React.forwardRef((props: Record<string, unknown>, ref: React.Ref<unknown>) => (
<Route>{routeProps => <Wrapped ref={ref} {...routeProps} {...props} />}</Route>
));
const name = Wrapped.displayName || Wrapped.name || 'Component';
WithRouterAndRef.displayName = `withRouterAndRef(${name})`;
return WithRouterAndRef;
}