简体   繁体   English

我在制作 loginPage 时从控制台收到错误,但我不知道出了什么问题(React.js)

[英]i got error from console while making loginPage but i don't know what's wrong (React.js)

 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.1/umd/react-dom.production.min.js"></script> import React,{useState} from "react"; import {connect} from "react-redux"; import {loginUser} from '../../actions/user_action' function RegisterLogin() { const [loginData, setLoginData] = useState({ email: "", password: "", errors: [], }); const displayErrors = (errors) => { errors.map((error,i)=> <p key={i}>{error}</p>) } const handleChange = (e) => { setLoginData({[e.target.name]: e.target.value}); } const submitForm = (e) => { e.preventDefault(); let dataToSubmit ={ email: loginData.email, password: loginData.password } if (isFormValid(loginData)){ setLoginData({errors: []}); this.props.dispatch(loginUser(dataToSubmit)).then(response => {console.log(response)}) } } const isFormValid = ({email,password}) => email&&password; return ( <div className="login-container"> <div className="row login-box"> <h2 className="login">Login</h2> <form className="column s12"> <div className="col"> <div className="input-field col s12"> {/* email */} <input name="email" value={loginData.email} onChange={e=>{handleChange(e)}} id="email" type="email" className="validate" placeholder="Email" /> <span className="helper-text" data-error="type a proper email" data-success="email is valid" /> </div> {/* password */} <div className="input-field col s12"> <input name="password" value={loginData.password} onChange={e=>{handleChange(e)}} id="password" type="password" className="validate" placeholder="Password" autoComplete="off" /> <span className="helper-text" data-error="wrong password" data-success="right" /> </div> </div> {/* errormessages */} {loginData.errors.length >0 &&( <div> {displayErrors(loginData.errors)} </div> )} {/* buttons */} <div > <div className="col s12"> <button className="btn waves-white black brighten-2" type="submit" name="action" onSubmit={submitForm} > Login </button> </div> </div> </form> </div> <div className="whatever">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.i need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logoi need a logo</div> </div> ); } const mapStateToProps = (state) => { return { user: state.user } } export default connect(mapStateToProps)(RegisterLogin);

above is my code.以上是我的代码。 and whenever i try to write on input tag I keep getting this error message:每当我尝试在输入标签上书写时,我都会收到此错误消息:

TypeError: Cannot read property 'length' of undefined
RegisterLogin
src/components/RegisterLogin/index.js:43
  40 | <div className="row login-box">
  41 | <h2 className="login">Login</h2>
  42 |   <form className="column s12">
> 43 |     <div className="col">
     | ^  44 |       <div className="input-field col s12"> 
  45 |       {/* email */}
  46 |       <input

However I have no clue about this error, can you inform me what is wrong?但是我不知道这个错误,你能告诉我哪里出了问题吗? thx for reading, your help will be appreciated感谢阅读,您的帮助将不胜感激

ps when i remove errors property in state and also remove ps 当我删除 state 中的错误属性并删除时

{loginData.errors.length >0 &&(
            <div>
              {displayErrors(loginData.errors)}
            </div> 

this code at least i can type something on input value but with those code i even can't type a single alphabet even tho i used handleChange function as onclick event on input tag这段代码至少我可以在输入值上输入一些东西但是使用这些代码我什至无法输入单个字母即使我使用 handleChange function 作为输入标签上的 onclick 事件

From what you have posted here, I would say the most likely issue is that loginData.errors is undefined when you attempt to evaluate the .length property of it.根据您在此处发布的内容,我想说最有可能的问题是loginData.errors在您尝试评估它的.length属性时undefined Since there is no undefined.length , you get a TypeError .由于没有undefined.length ,你会得到一个TypeError

The quickest way around this would be to use the optional chaining operator ( ?. ) when getting those properties so that the code will simply move on when it can't find the property:解决此问题的最快方法是在获取这些属性时使用可选的链接运算符 ( ?. ) ,以便代码在找不到该属性时继续前进:

{loginData?.errors?.length >0 && (
  <div>
    {displayErrors(loginData.errors)}
  </div> 
)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道“渲染”有什么问题 - I don't know what's wrong with 'render' 我不知道联系表有什么问题 - I don't know what's wrong with contact form TypeError: undefined is not a function(靠近'...(0,_react.useCallBack)...')。 我不知道我的代码中的 usCallBack() function 有什么问题 - TypeError: undefined is not a function (near '...(0, _react.useCallBack)...'). I don't know what's wrong with the usCallBack() function in my code 将JS代码转换为React.js,看不到我在做什么 - Converting JS code to React.js, can't see what I'm doing wrong 输入评论后出现关键错误。 我不知道如何解决(反应问题) - I got a key error after entering a comment. I don't know how to fix (react propblem) 我在一个简单的JS程序中犯了一个错误,但是我不知道为什么它是错误的 - I made a mistake in a simple JS program, but I don't know why it's wrong 当我点击按钮时,我不知道怎么了 - I don't know what's wrong when i click the button nothing work 我不知道我在哪里错了Js对象 - I don't know where i am wrong with Js objects JavaScript错误:未捕获的TypeError:undefined不是第4行上的函数; 我不知道我做错了什么 - JavaScript error: Uncaught TypeError: undefined is not a function on line 4; I Don't Know What I Did Wrong 我不知道我的编码有什么问题。 (javascript) - I don't know what's wrong with my coding. (javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM