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
6 changes: 5 additions & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ export type RouterStoreContext = {
location: Location;
};

type RouteComponentProps = Partial<RouteContext> & {
[k: string]: any;
};

export type Route = {
path: string;
exact?: boolean;

/** The component to render on match, typed explicitly */
component: ComponentType<RouteContext>;
component: ComponentType<RouteComponentProps> | ComponentType<any>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this just be made into an optional generic like in the withRouter case? This should only need to be supported for consumers that do not use the exported RouteComponent component, and I would avoid the use of any here as a result. The default prop type just just be RouteComponentProps for those using the route component.


/** If present, must return true to include the route. */
enabled?: () => boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export { MemoryRouter } from './memory-router';
export { StaticRouter } from './static-router';
export { RouterActions } from './router-actions';
export { Redirect } from './redirect';
export { withRouter } from './with-router';
export { withRouter, WithRouterProps } from './with-router';
export { useResource, useRouter, useRouterActions } from './hooks';
export { useResourceStoreContext } from './resource-store';
export { createResource } from './resource-utils';
Expand Down
16 changes: 10 additions & 6 deletions src/controllers/with-router/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React, { ComponentType } from 'react';

import { BrowserHistory, RouteContext } from '../../common/types';
import { RouterActionPush, RouterActionReplace } from '../router-store/types';
import { RouterSubscriber } from '../subscribers/route';

// TODO
type WithRouter = RouteContext & { history: BrowserHistory };
export type WithRouterProps = RouteContext & {
history: BrowserHistory;
push: RouterActionPush;
replace: RouterActionReplace;
};

const getWrappedComponentDisplayName = (
component: ComponentType<any>
Expand All @@ -23,12 +27,12 @@ const getWrappedComponentDisplayName = (
return `withRouter(${componentDisplayName})`;
};

export const withRouter = <P extends Record<string, any>>(
export const withRouter = <P extends Record<string, any> = {}>(
WrappedComponent: ComponentType<P>
) => {
): ComponentType<Omit<P, keyof WithRouterProps>> => {
const displayName = getWrappedComponentDisplayName(WrappedComponent);
const Component = WrappedComponent as ComponentType<WithRouter & P>;
const ComponentWithRouter = (props: P) => (
const Component = WrappedComponent;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/nit May as well just renamed the WrappedComponent argument to Component?

const ComponentWithRouter = (props: any) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't you use P anymore over any?

<RouterSubscriber>
{(
// @ts-ignore access private `history` store property
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
Redirect,
RouterActions,
withRouter,
WithRouterProps,
ResourceSubscriber,
useResource,
useRouter,
Expand Down