Type and export props from withRouter

In react-router v3.0.5, [`withRouter`][0] passes 4 props to the wrapped
component: `location`, `params`, `router`, and `routes`. These props are
different than those passed to `Route` components; these all come
directly from the Router either via `this.props` or `this.context`.

The component passed to `withRouter` are passed these props, but the
component returned from `withRouter` require only the component's own
props.

[0]: https://github.com/ReactTraining/react-router/blob/v3.0.5/modules/withRouter.js#L40
This commit is contained in:
Ross Allen
2017-08-03 16:49:26 -07:00
parent c9bd9cf538
commit 4023dce879
4 changed files with 17 additions and 8 deletions
+1
View File
@@ -13,6 +13,7 @@
// Huy Nguyen <https://github.com/huy-nguyen>
// Jérémy Fauvel <https://github.com/grmiade>
// Daniel Roth <https://github.com/DaIgeb>
// Ross Allen <https://github.com/ssorallen>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
+1
View File
@@ -34,6 +34,7 @@ export { RouteProps, PlainRoute } from "react-router/lib/Route";
export { IndexRouteProps } from "react-router/lib/IndexRoute";
export { RedirectProps } from "react-router/lib/Redirect";
export { IndexRedirectProps } from "react-router/lib/IndexRedirect";
export { WithRouterProps } from "react-router/lib/withRouter";
/* components */
export { default as Router } from "react-router/lib/Router";
+12 -2
View File
@@ -1,12 +1,22 @@
import { ComponentClass, StatelessComponent } from "react";
import { InjectedRouter, Params } from "./Router";
import { Location } from "history";
import { PlainRoute } from "./Route";
interface Options {
withRef?: boolean;
}
export interface WithRouterProps {
location: Location;
params: Params;
router: InjectedRouter;
routes: PlainRoute[];
}
type ComponentConstructor<P> = ComponentClass<P> | StatelessComponent<P>;
declare function withRouter<P, S>(component: ComponentConstructor<P> & S, options?: Options): ComponentClass<P> & S;
declare function withRouter<P>(component: ComponentConstructor<P>, options?: Options): ComponentClass<P>;
declare function withRouter<P, S>(component: ComponentConstructor<P & WithRouterProps> & S, options?: Options): ComponentClass<P> & S;
declare function withRouter<P>(component: ComponentConstructor<P & WithRouterProps>, options?: Options): ComponentClass<P>;
export default withRouter;
+3 -6
View File
@@ -20,7 +20,8 @@ import {
RouterContext,
LinkProps,
RedirectFunction,
RouteComponentProps
RouteComponentProps,
WithRouterProps
} from "react-router";
import { createHistory, History } from "history";
@@ -71,11 +72,7 @@ class Master extends Component {
}
}
interface DashboardProps {
router: InjectedRouter;
}
class Dashboard extends React.Component<DashboardProps> {
class Dashboard extends React.Component<WithRouterProps> {
static staticMethodToBeHoisted(): void { }
navigate() {