Make Route component extensible

Shouldn't have any impact whatsoever in existing codebases due to default generics
This commit is contained in:
mtpc
2017-07-04 21:17:49 +02:00
committed by Mtpc
parent 96617b0443
commit bc710a72d6
3 changed files with 18 additions and 1 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ export interface RouteProps {
exact?: boolean;
strict?: boolean;
}
export class Route extends React.Component<RouteProps> { }
export class Route<T extends RouteProps = RouteProps> extends React.Component<T> { }
export interface RouterProps {
history: any;
@@ -0,0 +1,16 @@
import * as React from 'react';
import { Route, RouteProps } from 'react-router';
interface CustomRouteInterface extends RouteProps {
aProperty: string;
anotherOne: boolean;
willItEnd?: number;
}
// React advocates composition over inheritance, but doesn't prevent us from using it
export default class CustomRoute extends Route<CustomRouteInterface> {
render() {
const maybeElement = super.render();
return maybeElement && <div className="meaningfulClass">{React.cloneElement(maybeElement, this.props)}</div>;
}
}
+1
View File
@@ -33,6 +33,7 @@
"test/NavigateWithContext.tsx",
"test/MemoryRouter.tsx",
"test/Switch.tsx",
"test/InheritingRoute.tsx",
"test/WithRouter.tsx"
]
}