简体   繁体   English

未捕获的TypeError:无法在react中读取null的属性'state'

[英]Uncaught TypeError: Cannot read property 'state' of null in react

I am trying to implement a simple signup page in react. 我正在尝试实现一个简单的注册页面。 However, when I try to submit the form, I get signup.js:53 Uncaught TypeError: Cannot read property 'state' of null 但是,当我尝试提交表单时,我得到了signup.js:53 Uncaught TypeError: Cannot read property 'state' of null

Apparently react is not properly setting the state. 显然反应不正确地设定状态。 Here is the code from the Signup component: 以下是Signup组件的代码:

import React, { Component } from 'react'; 从'react'导入React,{Component};

export default class Signup extends Component {

  constructor(props) {
    super(props)
    this.state = {
        username: "",
        password1: "",
        password2: "",
        error: ""
      }
    this.onChange = this.onChange.bind(this);
  }

  onChange(e) {
    this.setState({[e.target.name]: e.target.value})
  }

  handleSubmit(e) {
    e.preventDefault()
    console.log(e)
    var username = this.state.username.trim()
    var password1 = this.state.password1.trim()
    var password2 = this.state.password2.trim()

    if (!username || !password1 || !password2) 
      return
    else if (password2 !== password1)
      this.setState({
        error: "Passwords didn't match",
        username: "",
        password1: "",
        password2: ""
        })
  }
  render() {
    return (
      <div>
        <form className="signupForm" onSubmit={this.handleSubmit}>
          <input
            type="text"
            name="username"
            placeholder="Username"
            value={this.state.username}
            onChange={this.onChange} />
          <input
            type="text"
            name="password1"
            placeholder="Password"
            value={this.state.password1}
            onChange={this.onChange} />
          <input
            type="text"
            name="password2"
            placeholder="Password again"
            value={this.state.password2}
            onChange={this.onChange} />
          <input type="submit" value="Post" />
        </form>
        <div value={this.state.error}></div>
      </div>
      )
  }
}

You should set this for handleSubmit as you did for onChange 您应该像onChange一样为handleSubmit设置this

constructor(props) {
  super(props)
  this.state = {
    username: "",
    password1: "",
    password2: "",
    error: ""
  }
  this.onChange = this.onChange.bind(this);
  this.handleSubmit = this.handleSubmit.bind(this);
}

暂无
暂无

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

相关问题 反应-未捕获的TypeError:无法读取&gt; null的属性&#39;state&#39; - React - Uncaught TypeError: Cannot read property 'state' of > null 未捕获的类型错误:无法读取未定义的属性“状态”-React.js - Uncaught TypeError: Cannot read property 'state' of undefined - React.js react:uncaught TypeError:无法读取未定义的属性“state” - react: uncaught TypeError: Cannot read property 'state' of undefined 未捕获的TypeError:无法读取未定义的反应状态项的属性&#39;toUpperCase&#39; - Uncaught TypeError: Cannot read property 'toUpperCase' of undefined react state item 反应 State:未捕获的类型错误:无法读取未定义的属性“值” - React State: Uncaught TypeError: Cannot read property 'value' of undefined 反应未捕获的类型错误:无法读取 null 的属性“load” - REACT Uncaught TypeError: Cannot read property 'load' of null React tutorial:Uncaught TypeError:无法读取null的属性“data” - React tutorial: Uncaught TypeError: Cannot read property 'data' of null 反应选择:未捕获的类型错误:无法读取 null 的属性“offsetTop” - React-select: Uncaught TypeError: Cannot read property 'offsetTop' of null 未捕获的类型错误:无法读取 null React 的属性“setState” - Uncaught TypeError: Cannot read property 'setState' of null React 未被捕获的TypeError:无法在React中读取null的属性&#39;value&#39; - Uncaught TypeError: Cannot read property 'value' of null in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM