简体   繁体   English

React redux - 无法读取未定义的属性“状态”

[英]React redux - Cannot read property 'state' of undefined

import React, {Component} from "react";
import {connect} from "react-redux";
import PropTypes from "prop-types";
import "../App.css";
import {register} from "../actions/authActions"

class Register extends Component {

    state = {
      username: "",
      email: "",
      password: "",
      msg:null
    };

    static propTypes={
      isAuthenticated: PropTypes.bool,
      error: PropTypes.object.isRequired,
      register: PropTypes.func.isRequired
    };

    componentDidUpdate(previousProps){
      const {error} = this.props;
      if(error !== previousProps.error) {
        //Check for register error
        if(error.id === "REGISTER_FAIL") {
          this.setState({msg: error.msg.msg});
        } else {
          this.setState({msg: null});
        }
      }
    }

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

    onSubmit(e){
      e.preventDefault();

      const { username, email, password} = this.state;

      //Create User Obj
      const newUser = {
        username,
        email,
        password
      };

      //Attempt Register
      this.props.register(newUser);

    }

  
  
  render() {
    return (
      <div>
      {this.state.msg ? (alert(this.state.msg)): null}
      <form onSubmit={this.onSubmit}>
      <div className="container-register">
        <div className="container-form">
          <div className="form">
            <h1>Register</h1>
            <p>Please enter your information before you dive in to discussions.</p>

            <div className="form-group">
              <label for="email">Email</label>
              <br/>
              <input type="text" placeholder="myemail@email.com" name="email" id="email" onChange={this.onChange} />
            </div>

            <div className="form-group">
              <label for="username" >Username</label>
              <br/>
              <input type="text" placeholder="MyUniqueUsername" name="username" id="username" onChange={this.onChange} />
            </div>

            <div className="form-group">
              <label for="Password">Password</label>
              <br/>
              <input type="password" placeholder="Enter Password" name="password" id="password" onChange={this.onChange} />
            </div>

            <div className="form-group">
              <br/>
              <button type="submit">Register Me!</button>
            </div>

          </div>
        </div>
      </div>
      </form>
      </div>
    )
  }
}

const mapStateToProps = state => ({
  isAuthenticated: state.auth.isAuthenticated,
  error: state.error
});

export default connect(mapStateToProps, {register})(Register);

Hello there, I'm trying to register my email, username, password via using react and react-redux.你好,我正在尝试通过使用 react 和 react-redux 来注册我的电子邮件、用户名、密码。 I'm using axios on back with node + expressjs.我在后面使用 axios 和 node + expressjs。 I keep getting this, and don't know why.我一直收到这个,不知道为什么。 From my register function, I use axios.post to my back end.在我的注册功能中,我使用 axios.post 到我的后端。 (Passing my token, and user data for register). (传递我的令牌和用于注册的用户数据)。 https://i.imgur.com/KkojErR.png

Any ideas what is wrong with this?任何想法这有什么问题?

It looks like you forgot to bind this context to onSubmit method, you can do this by writing in arrow function without binding to this:看起来您忘记this上下文绑定到onSubmit方法,您可以通过编写箭头函数而不绑定到此来完成此操作:

onSubmit = (e) => {
 // it should work 
 const { username, email, password} = this.state;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM