Last active
December 16, 2019 16:31
-
-
Save ericchernuka/e08fa4499a19034eaebd81df8c74fe81 to your computer and use it in GitHub Desktop.
Component to handle nested URLs for linking
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
import React from 'react' | |
import { Link, LinkProps, useRouteMatch } from 'react-router-dom' | |
const VendorContextLink = (props: LinkProps) => { | |
const match = useRouteMatch<{ vendorId: string }>('/vendors/:vendorId') | |
if (!match) { | |
return <Link {...props} /> | |
} | |
const modifiedTo = typeof props.to === 'string' ? props.to : { | |
pathanme: `${match.url}/${props.to}`, | |
...props.to | |
} | |
return ( | |
<Link {...props} to={modifiedTo} /> | |
) | |
} | |
export default VendorContextLink |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment