ReactJS cannot fetch with state, error: "Cannot read property 'state' of null"

ReactJS cannot fetch with state, error: "Cannot read property 'state' of null"

我可以在 fetch 之外访问 this.state,但是当试图在 fetch 内部访问状态时,我得到这个错误:

Uncaught TypeError: Cannot read property 'state' of null

我查看了 Whosebug 上的其他帖子,但没有找到在获取调用中使用状态属性的可行解决方案。请帮忙

class SignIn extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      email: '',
      password: ''
    }
    this.handleValueChange = this.handleValueChange.bind(this);
  }

  handleValueChange(event) {
    const property = event.target.name;
    this.setState({[property]: event.target.value});
  }

  handleLogin(event){
    event.preventDefault();

    fetch("http://localhost:3001/login.json", {
        method: "POST",
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          email: this.state.email,  //this is where the error occurs
          password: '123456'
        })
    })
    .then(function(res){return res.json();})
    .then(function(data){console.log(data)});
  }

  render() {
    return(
      <header>
        <NavigationBar />

        <div className="form-box">
          <form onSubmit={this.handleLogin}>
            <h2>Login</h2>
            <br />
            <div className="row">
              <div>
                <label>Email</label>
                <input type="text" name="email" ref='emailInputField' 
                  onChange={(event) => this.handleValueChange(event)}
                  required
                />
              </div>
            </div>
            <br />
            <div className="row">
              <div>
                <label>Password</label>
                <input type="text" name="password"
                  onChange={(event) => this.handleValueChange(event)}
                  required
                />
              </div>
            </div>
            <br />
            <div className="btn-submit">
              <input type="submit" value="Let's go!" />
            </div>
          </form>
          <button onClick={() => console.log("this.state", this.state)} >Show state</button>
        </div>
      </header>
    )
  }
}

注意:为简洁起见,部分代码未显示

你有没有尝试过这样的事情:

handleLogin = (event) => {
    event.preventDefault();

    fetch("http://localhost:3001/login.json", {
        method: "POST",
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          email: this.state.email,  //this is where the error occurs
          password: '123456'
        })
    })
    .then((res) => {return res.json();})
    .then((data) => {console.log(data)});
  }

或者像评论中建议的那样将您的函数绑定到您的组件,就像您已经对其他函数所做的那样。箭头函数虽然更流畅。

在构造函数中添加 this.handleLogin,如下所示。

 constructor(props){
    super(props);
    this.state = {
      email: '',
      password: ''
    }
    this.handleValueChange = this.handleValueChange.bind(this);
    this.handleLogin = this.handleLogin.bind(this)
  }

我认为以下应该可行。

在 fetch(...) 上面的行中添加 const self = this; 并调用 self.state.email

如果有,请告诉我。