【问题标题】:./src/Components/ContactForm.jsx Line 37: 'sendEmail' is not defined no-undef./src/Components/ContactForm.jsx 第 37 行:'sendEmail' 未定义 no-undef
【发布时间】:2021-09-15 09:09:07
【问题描述】:

如果我的帖子格式不正确,我深表歉意,我在 JS/React 或 Stack 溢出方面都没有经验

这是我两天内的第 7 次迭代,试图让我的联系表单完全正常工作。我已经让它工作到提交电子邮件中唯一没有发送的项目是 TNC 和促销箱。

我需要 Checkboxes 来发送 true false,这取决于它们是否被选中。 如果我可以将真假转换为是和否,则可以加分

我得到的错误是

Failed to compile.

./src/Components/ContactForm.jsx
  Line 37:  'sendEmail' is not defined  no-undef

这是受影响的线路

<Form onSubmit={sendEmail}>

这是我的“sendMail”箭头函数

sendEmail = (e) => {
    e.preventDefault();
    emailjs.sendForm("test", "testTemp", e.target, "user_EPYkKnHwljQTCbJkzO7YD9")
      .then(
        (result) => {
          alert(+"Email sent successfully!");
          e.target.reset();
        },
        (error) => {
          alert("Email send failed... :( I had one job...");
        }
      );
  };

如果有任何帮助,我在https://test.ghostrez.net 上有一个旧的非功能性联系表单版本。

这里是完整的 'ContactForm.jsx',以防万一。

import React from "react";
import emailjs from "emailjs-com";
import { Component } from "react";
import { Button, Checkbox, Form, Input, TextArea } from "semantic-ui-react";

export default class ContactUs extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      TNC: false,
      promos: true,
    };
  }
  onToggle = () => {
    const TNC = !this.state.TNC;
    this.setState({ TNC });
  };
  onTogglePromos = () => {
    const promos = !this.state.promos;
    this.setState({ promos });
  };
  sendEmail = (e) => {
    e.preventDefault();
    emailjs.sendForm("test", "testTemp", e.target, "user_EPYkKnHwljQTCbJkzO7YD9")
      .then(
        (result) => {
          alert(+"Email sent successfully!");
          e.target.reset();
        },
        (error) => {
          alert("Email send failed... :( I had one job...");
        }
      );
  };
  render() {
    return (
      <Form onSubmit={sendEmail}>
        <Form.Group widths="equal">
          <Form.Field
            id="firstName"
            control={Input}
            label="First name"
            name="firstName"
            placeholder="First name"
            required
          />
          <Form.Field
            id="lastName"
            name="lastName"
            control={Input}
            label="Last name"
            placeholder="Last name"
          />
        </Form.Group>
        <Form.Group widths="equal">
          <Form.Field
            id="Email"
            control={Input}
            label="Email"
            name="email"
            placeholder="joe@schmoe.com"
            onChange=""
            required
          />
          <Form.Field
            id="Phone"
            control={Input}
            label="Phone"
            name="phone"
            placeholder="0469 420 689"
            required
          />
        </Form.Group>
        <Form.Field
          id="Message"
          control={TextArea}
          label="Message"
          name="message"
          placeholder="Message"
          required
        />
        <Form.Group widths="equal">
          <Form.Field>
            <Checkbox
              label="I agree to the Terms and Conditions"
              required
              control={Checkbox}
              data="TNC"
              onChange=''
            />
          </Form.Field>
          <Form.Field>
            <Checkbox
              label="Send me occasional updates and promotions"
              defaultChecked
              control={Checkbox}
              data="promos"
              onChange=''
            />
          </Form.Field>
        </Form.Group>
        <Form.Field
          id="Submit"
          control={Button}
          type="submit"
          value="submit"
          positive
          content="Submit"
        />
      </Form>
    );
  }
}

【问题讨论】:

    标签: javascript reactjs semantic-ui


    【解决方案1】:

    您在以下语句中指的是sendEmail

    <Form onSubmit={sendEmail}>
    

    但是,sendEmailContactUs 组件/实例的成员。您必须使用this.sendEmail 引用它。

    <Form onSubmit={this.sendEmail}>
    

    【讨论】:

    • 谢谢你克里斯,解决了这个问题...我已经添加了handleSubmit = (e) =&gt; { e.preventDefault(); if (this.state.TNC !== true) { return this.sendEmail; } else { return alert( "You need to accept the Terms and Conditions before proceeding." ); } }; render() { return ( &lt;Form onSubmit={this.handleSubmit}&gt;,它一直给我'Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.:(
    • 不错!关于新问题,请使用更新的代码发布新问题。在评论中无法阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2018-06-09
    • 2018-10-02
    • 2021-05-26
    • 2021-06-17
    • 2022-01-26
    相关资源
    最近更新 更多