Reputation: 8586
This sounds like it must have been asked before but I could only find how to do this in react native but I could not find how it's done in normal react for web. Preferably not with an a tag or Link tag that needs to be styled.
Here some code to illustrate what I want to do:
const onClickMailtoHandler = () => {
//TODO: open default e-mail client e.g. via mailto link with text from (state) variable as body
}
<Button onClick={onClickMailtoHandler}>Send E-Mail</Button>
Here is how to do a mailto link in HTML:
<a href="mailto:[email protected]?body=My custom mail body">E-Mail to Max Mustermann</a>
Upvotes: 32
Views: 104922
Reputation: 31
I achieved this in NextJS 14 by passing the attribute as={Link}
to the ListGroup component and the href
attribute as href='mailto:[email protected]'
import Link from next/link
import ListGroup from "react-bootstrap/ListGroup";
<ListGroup.Item as={Link} href='mailto:[email protected]' className={styles.listGroupItem}>
[email protected]
</ListGroup.Item>
Upvotes: 0
Reputation: 27
make any element in jsx click mail write this =
<div onClick={(e) => {window.location.href ='mailto:[email protected]';}}>any thing here </div>
just try it.
Upvotes: 3
Reputation: 201
I've used the following method for opening the default mail client when button is clicked:
<button onClick={() => window.location = 'mailto:[email protected]'}>Contact Me</button>
Upvotes: 20
Reputation: 8586
I ended up creating a component similar to what @GitGitBoom suggested in the comments.
Here for future Seekers:
import React from "react";
import { Link } from "react-router-dom";
const ButtonMailto = ({ mailto, label }) => {
return (
<Link
to='#'
onClick={(e) => {
window.location.href = mailto;
e.preventDefault();
}}
>
{label}
</Link>
);
};
export default ButtonMailto;
Use it like this:
<ButtonMailto label="Write me an E-Mail" mailto="mailto:[email protected]" />
Upvotes: 44
Reputation: 4681
Try this
<Link to='javascript:void(0)'
onClick={() => window.location = 'mailto:[email protected]'}>
Contact Me
</Link>
<OtherElement onClick={() => window.location = 'mailto:[email protected]'}>
Contact Me
</OtherElement>
Upvotes: 6
Reputation: 7980
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
(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
Upvotes: 0