【发布时间】:2021-07-01 16:46:46
【问题描述】:
一天多来我一直在分析这段代码并测试了几件事,但我仍然无法解决以下问题:
“为什么我的电子邮件验证功能中的电子邮件是空的?”
import { useState } from 'react';
import { FirestoreProvider, FirestoreMutation } from "@react-firebase/firestore";
import 'firebase/firestore';
import firebase from "firebase/app";
import '@firebase/analytics';
const config = {
...
};
export default function TextInput(){
const [email, setEmail] = useState("");
const collectionPath = "leads";
async function Submit(runMutation){
await runMutation({email});
}
function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
function ValidateAndSubmit(runMutation){
if(validateEmail(email)){
Submit(runMutation);
}else{
alert("Please type a valid email!");
}
}
function onKeyPress(key){
if(key === "Enter"){
ValidateAndSubmit();
}
}
return <FirestoreProvider {...config} firebase={firebase}>
<input type="email"
placeholder="Type your e-mail to receive early access to the product"
style={{width : 500, marginRight : 10}}
onKeyPress={event => onKeyPress(event.key)}
onChange={e => {
e.preventDefault();
setEmail(e.target.value)
}
}/>
<FirestoreMutation path={collectionPath} type="add">
{({ runMutation }) => (
<input type="submit" value="Submit" onClick={_ => ValidateAndSubmit(runMutation)}/>
)}
</FirestoreMutation>
</FirestoreProvider>
}
谁能确定问题出在哪里?
【问题讨论】:
-
您检查控制台错误了吗?
-
控制台没有错误。
-
在按键时你传递
ValidateAndSubmit()不带参数但在函数中你有runmutation参数。 -
是的,这可能会使代码在键盘快捷键部分不起作用,但我现在没有使用该部分,我只是使用提交按钮
-
{({ runMutation }) => ...是如何调用的?
标签: reactjs react-hooks react-functional-component