Last active
January 27, 2019 11:31
-
-
Save robjtede/b515d7587ec710e605b6dc24af1afb84 to your computer and use it in GitHub Desktop.
The Real "RouteWithProps" React Component in TypeScript Code Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Route path="/monitor" component={props => | |
<App data={linksData} {…props} /> | |
}> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RouteWithProps | |
path="/account" | |
component={AccountDetails} | |
withProps={{ details: this.state.details }} | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Props extends RouteComponentProps<{}> { | |
details: AccountDetails | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface RouteWithPropsProps<P> extends RouteProps { | |
component: ComponentType<P> | |
withProps: P | |
} | |
export const RouteWithRoute = <P extends object>({ | |
component: WrappedComponent, | |
withProps, | |
...routeProps | |
}: RouteWithPropsProps<P>): ReactNode => { | |
return ( | |
<Route | |
{...routeProps} | |
render={injectedProps => { | |
return <WrappedComponent {...injectedProps} {...withProps} /> | |
}} | |
/> | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type PropsRouteProps<P> = P extends RouteComponentProps<any> | |
? RouteProps & { | |
component: ComponentType<P> | |
withProps?: Omit<P, keyof RouteComponentProps<any>> | |
} | |
: never | |
export const PropsRoute = <P extends RouteComponentProps<any>>({ | |
component: WrappedComponent, | |
withProps, | |
...routeProps | |
}: PropsRouteProps<P>): ReactElement<P> => { | |
return ( | |
<Route | |
{...routeProps} | |
render={childProps => { | |
return <WrappedComponent {...childProps} {...withProps} /> | |
}} | |
/> | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type PropsRouteProps<P> = P extends WithDataProps<any> & RouteComponentProps<any> | |
? RouteProps & { | |
component: ComponentType<P> | |
withProps?: Omit<P, keyof RouteComponentProps<any> | keyof WithDataProps<any>> | |
} | |
: P extends RouteComponentProps<any> | |
? RouteProps & { | |
component: ComponentType<P> | |
withProps?: Omit<P, keyof RouteComponentProps<any>> | |
} | |
: never |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment