gpt4 book ai didi

reactjs - 不可见的 reCAPTCHA 在 next.js 应用程序的页面导航中消失

转载 作者:行者123 更新时间:2023-12-05 04:28:18 25 4
gpt4 key购买 nike

我在 3 个单独的页面上有 reCAPTCHA。它们在第一次加载时看起来很完美。但是当我导航到另一个页面然后再次重新导航回该页面(具有验证码的页面)时,它们就会消失。我的应用程序是单页应用程序。我正在使用下一个/链接从一个页面导航到另一个页面。

这是我的联系页面:

import { useState, useEffect, useRef } from "react";
import { pattern } from "./pricing";
import styles from "./../styles/Contact.module.css";
import { post } from "../helper";
import { CONTACT_PAGE_ACTION_EVENT } from "../constant";
import ClipLoader from "react-spinners/ClipLoader";
import ReCAPTCHA from "react-google-recaptcha";


function Contact({captcha_site_key}) {
const [formData, setFormData] = useState({
firstName: "",
email: "",
emailError: "",
phone: "",
message: "",
responseText: ""
});
const recaptchaRef = useRef(null);
const [disabled, setDisabled] = useState(true);
const [loading, setLoading] = useState(false)

const formDataHandler = (name, value) => {
setFormData((prev) => {
if (name === "email") {
return {
...prev,
[name]: value.trimStart(),
["emailError"]: pattern.test(value.trimStart())
? ""
: "That is Not a valid email",
};
} else {
return {
...prev,
[name]: value.trimStart(),
};
}
});
};

useEffect(() => {
setDisabled(
!(
formData.email.length > 0 &&
formData.firstName.length > 0 &&
formData.message.length > 0 &&
formData.phone.length > 0 &&
formData.emailError.length === 0
)
);
}, [formData]);

const sendData = async(e) => {
e.preventDefault();
setLoading(true)
const token = await recaptchaRef.current.executeAsync();
recaptchaRef.current.reset();
const {emailError, responseText, ...data} = formData;

// stripping off the html from string
const secureData = {
firstName: data.firstName.replace(/(<([^>]+)>)/gi, "").replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''),
email: data.email.replace(/(<([^>]+)>)/gi, "").replace(/[`~!#$%^&*()_|+\-=?;:'",<>\{\}\[\]\\\/]/gi, ''),
phone: data.phone.replace(/(<([^>])>)/gi, "").replace(/[`~!@#$%^&*()_|\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''),
message: data.message.replace(/(<([^>]+)>)/gi, "").replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''),
token
}
const result = await post(secureData, CONTACT_PAGE_ACTION_EVENT)

if(result.status === 'success'){
setFormData({
firstName: "",
email: "",
emailError: "",
phone: "",
message: "",
responseText: result?.status
})
}else {
setFormData(prev => {
return {...prev, ['responseText']: result?.status}
})
}
setLoading(false)

}
return (
<section className={styles.contact_container}>
<ReCAPTCHA
ref={recaptchaRef}
size="invisible"
sitekey={captcha_site_key}
/>
<section className={styles.contact_section}>
<form>
<h3> Let&apos;s Talk </h3>
<p> Fill out the form to send us a message </p>

{/* FirstName */}
<div className="form-floating mb-3">
<input
className="form-control shadow-none border-dark rounded-0"
id="floatingInput"
value={formData.firstName}
style={{ backgroundColor: loading ? '#f1f1f1' : 'white' }}
disabled={loading}
required
type="text"
name="firstName"
placeholder="First Name*"
onChange={(e) =>
formDataHandler(e.target.name, e.target.value.trimStart())
}
/>
<label htmlFor="floatingInput" className="text-muted">First Name*</label>
</div>

{/* Email */}
<div>
<div className="form-floating">
<input
className="form-control shadow-none border-dark rounded-0"
id="floatingInput"
value={formData.email}
style={{ backgroundColor: loading ? '#f1f1f1' : 'white' }}
disabled={loading}
required
type="email"
name="email"
placeholder="Email*"
onChange={(e) =>
formDataHandler(e.target.name, e.target.value.trimStart())
}
/>
<label htmlFor="floatingInput" className="text-muted">Email*</label>
</div>
<p> {formData.emailError} </p>
</div>

{/* Phone Number */}
<div className="form-floating mb-3">
<input
className="form-control shadow-none border-dark rounded-0"
id="floatingInput"
value={formData.phone}
required
disabled={loading}
style={{ backgroundColor: loading ? '#f1f1f1' : 'white' }}
type="text"
placeholder="Phone Number*"
name="phone"
onChange={(e) =>
formDataHandler(e.target.name, e.target.value.trimStart())
}
/>
<label htmlFor="floatingInput" className="text-muted">Phone Number*</label>
</div>

{/* Phone Number */}
<div className="form-floating mb-3">
<input
className="form-control shadow-none border-dark rounded-0"
id="floatingInput"
value={formData.message}
required
type="text"
disabled={loading}
style={{ backgroundColor: loading ? '#f1f1f1' : 'white' }}
placeholder="Message"
name="message"
onChange={(e) =>
formDataHandler(e.target.name, e.target.value.trimStart())
}
/>
<label htmlFor="floatingInput" className="text-muted">Message*</label>
</div>

{/* Send Button */}
<button
disabled={disabled || loading}
style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
opacity: disabled ? 0.5 : 1,
backgroundColor:"#ff6600",
cursor: disabled ? "auto" : "pointer",
width: 150
}}
onClick={(e) => sendData(e)}
>
<span style={{marginRight: 5}}> Send </span>
<ClipLoader color={'white'} loading={loading} size={15} />
</button>
<p
style={{
color: formData.responseText === 'error' ? 'red' : 'green',
textAlign: 'center',
height: 16,
marginTop: 10
}}
> {formData.responseText} </p>
</form>
</section>
</section>
);
}

export async function getStaticProps() {
return {
props: {
captcha_site_key: process.env.RECAPTCHA_SITE_KEY
}
}
}

export default Contact;

最佳答案

您的应用是否有 _document文件匹配 Next 的存储库?一旦我添加了 MyDocument 类,它就对我有用,如下所示:

import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components'

export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage

try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
})

const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
}
} finally {
sheet.seal()
}
}
}

关于reactjs - 不可见的 reCAPTCHA 在 next.js 应用程序的页面导航中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72644727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com