简体   繁体   English

这个反应代码中的错误是什么? 当我在第 1 部分中调用 function 时它不起作用但第 2 部分有效

[英]What is the error in this code of react? When i call the function in part 1 it doesnt work but part 2 works

This is my code.这是我的代码。 when i use function in part 1 it shows nothing on console.当我在第 1 部分中使用 function 时,它在控制台上没有显示任何内容。 but when i use function in part 2 it shows 'Hello'.但是当我在第 2 部分中使用 function 时,它显示“你好”。 but i cant see any differences.但我看不出有什么不同。 What am i doing wrong in part 1?我在第 1 部分做错了什么?

    handleChange = (e) => {
        this.setState({
            [e.target.id]:e.target.value
        })
    }
    // part 1
    handleSubmit = (e) => {
        e.prevendDefault()
        console.log(this.state)
        console.log("hello")
    }
    // part 2
    handleSubmit = (e) => {
        e.preventDefault();
        console.log(this.state)
        console.log('hello')
      }
    render() {
        return (
            <div className="form-group container w-50">
                <form onSubmit={this.handleSubmit}>
                    <label htmlFor="">Email</label>
                    <input type="email" id="email"className="form-control" onChange={this.handleChange}  />

                    <label htmlFor="">Password</label>
                    <input type="password" id="password" className="form-control" onChange={this.handleChange}/>

                    <button type="submit" className="btn btn-primary" >Submit</button>
                </form>


            </div>
        );
    }

There is a spelling mistake有一个拼写错误

   handleSubmit = (e) => {
       e.prevendDefault()
       console.log(this.state)
       console.log("hello")
   }

it's preventDefault() but you had prevendDefault .它是preventDefault()但你有prevendDefault

I would suggest using an IDE or editor with IntelliSense or autocomplete feature to prevent these kinds of mistakes in the future.我建议使用 IDE 或具有IntelliSense或自动完成功能的编辑器,以防止将来出现此类错误。

暂无
暂无

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

相关问题 function 部分不运行 - Part of the function doesnt run 如何在代码中此注释的部分中调用函数? - How can I call a function in the part where this comment in the code? 如何使 function 中的一部分代码在 state 更新后运行,或者在本机反应中未定义时运行? - How can I make a part of code inside the function to run after the state updates or when it is not undefined in react native? 错误:当我将一小部分 REACT 代码修改为 REDUX 时,“导入”和“导出”可能仅出现在顶层 - Error: 'import' and 'export' may only appear at the top level when I reworked small part of REACT code to REDUX 我的 function 的第二部分无法正常工作,但运行时没有显示错误 - I can't get the second part of my function to work, but there is no error shown when it's run function 组件的一部分不会在 React 中呈现,即使相同的代码适用于另一个组件 - Part of function Component wont render in React even though same code works for another component 代码的第二部分比第一部分更早地工作 - Second part of code works earlier then the first part 使用Jquery UI使可拖动函数正常工作的代码的基本部分是什么? - What is the essential part of code to make draggable function to work well using Jquery UI? 为什么代码的第一部分不能正常工作,而第二部分却可以工作? - Why does the first part of the code not work correctly while the second part works? 为什么 Ajax 调用在特定部分代码后不起作用? - Why doesn't the Ajax call work after a certain part of the code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM