I am not understanding why you are adding script in public/index.html
. If you are working with React then you don't need to add any script in HTML.
Points to Remember while implementing emailJS in React:
@emailjs/browser
useRef
hook.ref
in form properly.name
attribute in input element should exactly match with what you're using in emailJS website. In sendEmail
function, I am not seeing any error. I recommend you to recheck html form and follow the code snippet I have attached below.
You can run the following command in terminal to install it.
npm install @emailjs/browser --save
Then you can follow this code snippet to implement.
import React, { useRef } from 'react'; import emailjs from '@emailjs/browser'; export const ContactUs = () => { const form = useRef(); const sendEmail = (e) => { e.preventDefault(); emailjs.sendForm('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', form.current, 'YOUR_USER_ID').then((result) => { console.log(result.text); }, (error) => { console.log(error.text); }); }; return ( <form ref={form} onSubmit={sendEmail}> <label>Name</label> <input type="text" name="user_name" /> <label>Email</label> <input type="email" name="user_email" /> <label>Message</label> <textarea name="message" /> <input type="submit" value="Send" /> </form> ); };
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.