反应打开邮件到电子邮件客户端 onClick 与来自 textarea 的正文

React open mailto E-Mail client onClick with body from textarea

这听起来好像以前肯定有人问过,但我只能找到如何在本机反应中做到这一点,但我找不到在正常的网络反应中是如何做到的。最好 not 带有 a 标签或 Link 需要样式化的标签。

这里有一些代码来说明我想做什么:

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>

以下是在 HTML 中发送邮件至 link 的方法:

<a href="mailto:max.mustermann@example.com?body=My custom mail body">E-Mail to Max Mustermann</a>

我最终创建了一个类似于@GitGitBoom 在评论中建议的组件。

未来的求职者:

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;

这样使用:

<ButtonMailto label="Write me an E-Mail" mailto="mailto:no-reply@example.com" />

基于@CodingYourLife 的解决方案和这个问题的主题,我根据我的两个需求制作了我的组件版本。我将 react-router-dom 行为与 <A> html 标签的原生行为相结合。


Link 个组件的文件

  • index.tsx
  • types.ts
  • styles.ts

index.tsx(下)

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(下)

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(下)

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;
};

使用的库:material-ui

我使用以下方法在单击按钮时打开默认邮件客户端:

<button onClick={() => window.location = 'mailto:yourmail@gmail.com'}>Contact Me</button>

试试这个

<Link to='javascript:void(0)'
      onClick={() => window.location = 'mailto:yourmail@domain.com'}>
  Contact Me
</Link>
<OtherElement onClick={() => window.location = 'mailto:yourmail@domain.com'}>
  Contact Me
</OtherElement>