【问题标题】:React open mailto E-Mail client onClick with body from textarea使用来自 textarea 的正文响应打开的邮件到电子邮件客户端 onClick
【发布时间】:2020-12-26 04:03:02
【问题描述】:

这听起来之前一定有人问过,但我只能找到如何在 react native 中执行此操作,但 我无法找到在正常的 web 响应中是如何完成的。最好是 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 中创建 mailto 链接:

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

【问题讨论】:

  • 使用 a-tag (&lt;a /&gt;) 在语义上是正确的。将某些样式应用于 that 元素是否存在问题(与可能应用于底层button 元素的样式相反)?
  • 您是在问如何以编程方式打开 mailTo (stackoverflow.com/questions/21461589/…) 还是从 TextArea 获取文本值?
  • @GitGitBoom 是的,我最终只是用 window.location 来做这件事。如果您这样提交,我会批准它作为答案:)
  • 其他答案的链接并不是真正的答案,但您当然可以通过为链接的答案投票来表达您的感激之情。

标签: reactjs mailto


【解决方案1】:

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

这里为未来的求职者:

import React from "react";
import { Link } from "react-router-dom";

const ButtonMailto = ({ mailto, label }) => {
    return (
        <Link
            to='#'
            onClick={(e) => {
                window.location = mailto;
                e.preventDefault();
            }}
        >
            {label}
        </Link>
    );
};

export default ButtonMailto;

像这样使用它:

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

【讨论】:

    【解决方案2】:

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

    <Button onClick={() => window.location = 'mailto:yourmail@gmail.com'}>ContactMe</Button>
    </LeftSection>
    

    【讨论】:

      【解决方案3】:

      基于@CodingYourLife 的解决方案和这个问题的主要主题,我根据我的两个需求制作了我的组件版本。我将react-router-dom 行为与&lt;A&gt; 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

      【讨论】:

        【解决方案4】:

        试试这个

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

        【讨论】:

        • 对打字有什么想法吗? Type 'string' is not assignable to type 'Location'
        猜你喜欢
        • 2013-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-20
        • 1970-01-01
        • 1970-01-01
        • 2015-12-15
        • 2012-10-24
        相关资源
        最近更新 更多