[英]Please help to solve the problem React Hook "useRef"
Hello please help to solve the problem.您好,请帮助解决问题。
Page about contact form.关于联系表格的页面。
I must get data from with useRef to const {name, email, message}.我必须从 useRef 到 const {name、email、message} 获取数据。 And I must definitely use useRef
我必须绝对使用 useRef
It is error message, and I fixed photo error page:这是错误消息,我修复了照片错误页面:
src/components/Contact/Input.jsx Line 33:38: React Hook "useRef" is called in function "handleSubmit" that is neither a React function component nor a custom React Hook function. src/components/Contact/Input.jsx 第 33:38 行:React Hook “useRef”在函数“handleSubmit”中被调用,该函数既不是 React 函数组件也不是自定义 React Hook 函数。 React component names must start with an uppercase letter react-hooks/rules-of-hooks
React 组件名称必须以大写字母 react-hooks/rules-of-hooks 开头
Search for the keywords to learn more about each error.搜索关键字以了解有关每个错误的更多信息。
How I can fix this problem.我该如何解决这个问题。
It is source code:它是源代码:
import React, { useState, useRef } from "react";
import { makeStyles } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";
import Button from "@material-ui/core/Button";
const useStyles = makeStyles((theme) => ({
root: {
"& > *": {
margin: theme.spacing(1),
width: "25ch",
},
},
}));
const Form = () => {
const classes = useStyles();
//
const [status, setStatus] = useState("Submit");
const handleSubmit = async (e) => {
e.preventDefault();
setStatus("Sending...");
const { name, email, message } = useRef("");
let details = {
name: name.value,
email: email.value,
message: message.value,
};
let response = await fetch("http://localhost:5000/contact", {
method: "POST",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: JSON.stringify(details),
});
setStatus("Submit");
let result = await response.json();
alert(result.status);
};
//
return (
<div>
<form className={classes.root} onSubmit={handleSubmit}>
<TextField
type="text"
style={{ color: "white" }}
inputRef={this.name}
id="standard-basic"
label="Name"
/>
<TextField
inputRef={this.email}
required
type="email"
style={{ color: "white" }}
id="standard-basic"
label="Email"
/>
<TextField
inputRef={this.message}
type="text"
style={{ color: "white" }}
id="standard-basic"
label="Message"
required
/>
<Button type="submit" id="contacts1" color="primary">
{status}
</Button>
</form>
</div>
);
};
export default Form;
Man i had the same issues, here how i fixed the problem:伙计,我遇到了同样的问题,这里是我如何解决问题的:
In your case:在你的情况下:
import { useState as UseState, useRef as UseRef } from "react";
.....
const { name, email, message } = UseRef("");
your function "handleSubmit" starts with a small letter, you have to change it to "HandleSubmit".您的函数“handleSubmit”以小写字母开头,您必须将其更改为“HandleSubmit”。 it should work
它应该工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.