Created
May 10, 2020 17:16
-
-
Save tranlehaiquan/362e241dae0f796a4e6c29312cbc826e to your computer and use it in GitHub Desktop.
integrate Link react router to material ui Link
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, { RefObject } from 'react'; | |
import { Link as RouterLink, LinkProps as RouterLinkProps } from 'react-router-dom'; | |
import Link from '@material-ui/core/Link'; | |
import { LinkProps } from '@material-ui/core'; | |
type refType = ((instance: HTMLAnchorElement | null) => void) | RefObject<HTMLAnchorElement> | null | undefined; | |
type CustomLinkProps = RouterLinkProps & LinkProps; | |
const LinkBehavior = React.forwardRef((props: CustomLinkProps, ref: refType) => { | |
const { to, ...restProps } = props; | |
return (<RouterLink ref={ref} to={to} {...restProps} />) | |
}); | |
/** | |
* Custom Link with behavior of Link react router | |
* and style of material ui | |
*/ | |
const CustomLink: React.FunctionComponent<CustomLinkProps> = ({underline = 'none', children, ...restProps}) => { | |
return <Link component={LinkBehavior} {...restProps} underline={underline}>{children}</Link> | |
} | |
export default CustomLink; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment