I have successfully converted my HTML and CSS components to React, but I am facing some challenges with the JS part. I attempted to use react-helmet, but encountered an error:
Cannot read property 'addEventListener' of null
Here is my React.js file:
import React,{ Component }from 'react'
import { Helmet } from 'react-helmet'
import './ContactForm.css'
export default class ContactForm extends Component {
render() {
return (
<>
<section>
<div className="container">
<form action="https://formsubmit.co/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e18c98a18c98848c80888dcf828e8c">[email protected]</a>" method="POST" id="my-form"&...
</div>
</div>
</pre>
<p>Original HTML/CSS/JS code:</p>
<p><div>
<div>
<pre class="lang-js"><code>window.addEventListener("DOMContentLoaded", function () {
// get the form elements defined in your form HTML above
var form = document.getElementById("my-form");
// var button = document.getElementById("my-form-button");
var status = document.getElementById("status");
// Success and Error functions for after the form is submitted
function success() {
form.reset();
status.classList.add("success");
status.innerHTML = "Thanks!";
}
function error() {
status.classList.add("error");
status.innerHTML = "Oops! There was a problem.";
}
// handle the form submission event
form.addEventListener("submit", function (ev) {
ev.preventDefault();
var data = new FormData(form);
ajax(form.method, form.action, ...
etc.