【问题标题】:handlesubmit is undefined when i am writing the code in react functional component当我在反应功能组件中编写代码时,handlesubmit 未定义
【发布时间】:2020-11-07 14:01:57
【问题描述】:

这是我使用反应功能组件编写的寄存器组件的代码。我错过了什么,反应状态?代码如下。

import React, {useState} from 'react'
import './Register.css'
import axios from 'axios'

function Register() {
const [name, setName] = useState('');
const [email, setEmail] = useState(''); 
const [password, setPassword] = useState(''); 
const [confirmPassword, setConfirmPassword] = useState(''); 

     const handleSubmit = (e) => {
        e.preventDefault();
        const data = {name , email, password, confirmPassword};
     
        console.log('data');
       
     }
     
    return (
        <div className="signup__container">

            <div className="signup__containerInfo">
            <h1 className="h1__swari">सवारी नविकरणको लागि</h1>
            <hr />
            </div>


<form4 onSubmit={handleSubmit}>
<h5 className="h5__form"> Name</h5>
<input type="text" placeholder="पुरा नाम लेख्नुहोस्"  value={name} onChange={e => setName(e.target.value)}/>
<h5 className="h5__form"> Email-Address </h5>
<input type="Email" placeholder="ईमेल लेख्नुहोस्" value={email} onChange={e => setEmail(e.target.value)}/>
<h5 className="h5__form"> Password </h5>
<input type="Password" placeholder="पासवर्ड लेख्नुहोस्s" value={password} onChange={e => setPassword(e.target.value)}/>
<h5 className="h5__form"> Confirm Password </h5>
<input type="Password" placeholder="पुन: पासवर्ड लेख्नुहोस्" value={confirmPassword} onChange={e => setConfirmPassword(e.target.value)} />
<p>
<button type="submit" className="signup__registerButton" >Register Now</button>
</p>
</form4>
        </div>
    )
    }

export default Register

我认为所有代码都很好,但只有句柄提交说错误。我是新手,请帮忙。

【问题讨论】:

  • 不仅你缺少状态,没有这样的HTML元素form4
  • 你是直接改变状态而不是让 React 处理它吗??
  • 请定义const [name,setName] = useState('');,然后定义onChange={e=&gt;setName(e.target.value)}。否则,您正在改变不是实践的状态。
  • 有多种表格我累了给他们一个数字
  • @Ankush Verma 我添加了你告诉我仍然得到同样的错误。你能展示编码的解决方案吗?会有很大的帮助!

标签: reactjs forms


【解决方案1】:

第 7 行应该是

const handleSubmit = (e) => {

【讨论】:

  • 没有错误,但我无法在控制台中打印数据,这是该程序从注册表单的主要目标。
猜你喜欢
  • 1970-01-01
  • 2023-02-21
  • 2021-06-24
  • 2021-05-15
  • 1970-01-01
  • 2021-10-16
  • 2019-08-29
  • 1970-01-01
  • 2021-12-13
相关资源
最近更新 更多