Based on solution of @CodingYourLife and main topic of this problem i made my version of component based on two of my needs. I combined react-router-dom
behavior with native behavior of <A>
html tag.
Files of Link
component
- index.tsx
- types.ts
- styles.ts
index.tsx
(below)
import * as React from 'react';
import Typography from '@material-ui/core/Typography';
import { withStyles } from '@material-ui/core';
import { Link as RouterLink } from 'react-router-dom';
import styles from './styles';
import { IProps } from './types';
/**
* Just a wrapper in order to style properly
*
* - router link
* - native html <a> link
*/
class Link extends React.PureComponent<IProps> {
render(): JSX.Element {
const {
classes,
href,
children,
...routerLinkProps
} = this.props;
if (typeof href === 'string') {
return (
<a href={href}>
<Typography
className={classes.value}
>
{children}
</Typography>
</a>
);
}
return (
<RouterLink
to={'#'} // for <a> link default value because it's required by its lib
{...routerLinkProps}
>
<Typography
className={classes.value}
>
{children}
</Typography>
</RouterLink>
);
}
}
export default withStyles(styles)(Link);
styles.ts
(below)
import { Theme, createStyles } from '@material-ui/core';
export default (theme: Theme): ReturnType<typeof createStyles> => createStyles({
value: {
display: 'inline-block',
color: theme.palette.secondary.main,
fontWeight: 500,
'&:hover': {
textDecoration: 'underline',
},
},
});
types.ts
(below)
import {
WithStyles,
} from '@material-ui/core';
import { LinkProps } from 'react-router-dom';
import styles from './styles';
export type IProps = WithStyles<typeof styles> & Partial<LinkProps> & {
href?: string;
};
Used library: material-ui
<a />
). Is there an issue with applying some style to that element (as opposed to the style that is likely applied to the underlyingbutton
element)? – Levasseur