简体   繁体   English

反应如何修复失败的道具类型 - 类型字符串预期对象的无效道具

[英]React How to fix Failed prop type - Invalid prop of type string expected object

I'm building a small react -app where among other things users can register, login and so on.我正在构建一个小型react -app,用户可以在其中注册、登录等。 For the Login, I have created a loginform with additional error validation, coming from the backend.对于登录,我创建了一个带有额外错误验证的登录表单,来自后端。 Now when I try to login, and by purpose enter wrong credentials, nothing happens (which in some reason is right) but instead of my error messages, the console tells me about an error:现在,当我尝试登录并故意输入错误的凭据时,没有任何反应(出于某种原因是正确的),但控制台告诉我错误消息,而不是我的错误消息:

Failed prop type: Invalid prop `errors` of type `string` supplied to `Login`, expected `object`

Here is the code:这是代码:

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

class Login extends Component {
  constructor() {
   super();
   this.state = {
     email: "",
     password: "",
     errors: {}
   };

   this.onChange = this.onChange.bind(this);
   this.onSubmit = this.onSubmit.bind(this);
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.auth.isAuthenticated) {
      this.props.history.push("/mysite");
    }

    if (nextProps.errors) {
     this.setState({ errors: nextProps.errors });
    }
  }

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

  onSubmit(e) {
    e.preventDefault();

    const userData = {
      email: this.state.email,
      password: this.state.password
    };

    this.props.loginUser(userData);
  }

  render() {
    const { errors } = this.state;

    return (
      <div className="login">
        <div className="container">
          <div className="row">
            <div className="col-md-8 m-auto">
              <h1 className="display-4 text-center">Log In</h1>
               <p className="lead text-center">Sign in to your account</p>
               <form noValidate onSubmit={this.onSubmit}>
                 <div className="form-group">
                  <input
                   type="email"
                     className={classnames("form-control form-control-lg", {
                  "is-invalid": errors.email
                   })}
                  placeholder="Email Address"
                  name="email"
                  value={this.state.email}
                  onChange={this.onChange}
                 />
                 {errors.email && (
                   <div className="invalid-feedback">{errors.email}</div>
                 )}
               </div>
               <div className="form-group">
                 <input
                  type="password"
                  className={classnames("form-control form-control-lg", {
                  "is-invalid": errors.password
                  })}
                  placeholder="Password"
                  name="password"
                  value={this.state.password}
                  onChange={this.onChange}
                 />
                {errors.password && (
                  <div className="invalid-feedback">{errors.password}</div>
                )}
              </div>
              <input type="submit" className="btn btn-info btn-block mt-4" />
             </form>
           </div>
         </div>
       </div>
     </div>
    );
  }
}

Login.propTypes = {
  loginUser: PropTypes.func.isRequired,
  auth: PropTypes.object.isRequired,
  errors: PropTypes.object.isRequired
};

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

export default connect(
  mapStateToProps,
 { loginUser }
)(Login);

I have no clue why that error appears!?我不知道为什么会出现这个错误!? Can someone help me out?有人可以帮我吗?

You are passing string as an error instead of object in the props for the Login component.您将字符串作为错误而不是对象传递到登录组件的道具中。 Try console.log of "errors" in the component where Login component is rendered to see what value is getting set.在呈现登录组件的组件中尝试“错误”的 console.log 以查看设置的值。

PropTypes expecting an object because of your propTypes definition由于您的 propTypes 定义,PropTypes 需要一个对象

  erros: PropTypes.object.isRequired,

Use:用:

Login.propTypes = {
  loginUser: PropTypes.func.isRequired,
  auth: PropTypes.object.isRequired,
  errors: PropTypes.string.isRequired
};

If it's no required your also have to define a defaultProp:如果不需要,您还必须定义一个 defaultProp:

Login.propTypes ={
  errors: PropTypes.string,
}

Login.defaultProps = {
  errors: '',
}

Make sure your link is not misspelled,确保您的链接没有拼错,

export const loginUser=(userData)=>dispatch=>{
  axios.post('/api/users/login',userData)

not不是

export const loginUser=(userData)=>dispatch=>{
  axios.post('/api/user/login',userData) 

users not user用户不是用户

暂无
暂无

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

相关问题 警告:propType失败:使用React的类型为`array` expected`object`的prop - Warning: Failed propType: Invalid prop of type `array` expected `object` with React reactjs 失败的道具类型:提供给“ForwardRef(IconButton)”的“object”类型的无效道具“className”,应为“string” - reactjs Failed prop type: Invalid prop `className` of type `object` supplied to `ForwardRef(IconButton)`, expected `string` 道具类型失败:提供给“按钮”的对象类型无效的道具“标签”,应为“字符串” - Failed prop type: Invalid prop `label` of type `object` supplied to `Button`, expected `string` 警告:失败的道具类型:提供给“路由”的“对象”类型的无效道具“组件”,预期的“函数”使用 react-router-dom 错误 - Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function` Error using react-router-dom React Native SearchBar错误:道具类型失败:提供给`ForwardRef(TextInput)`的`array`类型的无效道具`value`,预期`string` - React Native SearchBar Error: Failed prop type: Invalid prop `value` of type `array` supplied to `ForwardRef(TextInput)`, expected `string` 失败的道具类型:向“表”提供了“功能”类型的道具“ rowSelection”无效,预期是“对象” - Failed prop type: Invalid prop `rowSelection` of type `function` supplied to `Table`, expected `object` 道具类型失败:提供给“ForwardRef(DataGrid)”的“object”类型的无效道具“rows”,预期为“array” - Failed prop type: Invalid prop `rows` of type `object` supplied to `ForwardRef(DataGrid)`, expected `array` 道具类型失败:提供给Styled(Container)的类型为number的道具类型无效,预期对象 - Failed prop type: Invalid prop `style` of type `number` supplied to `Styled(Container)`, expected `object` 道具类型失败:提供给“ForwardRef(TablePagination)”的“string”类型的道具“count”无效,应为“number” - Failed prop type: Invalid prop `count` of type `string` supplied to `ForwardRef(TablePagination)`, expected `number` 警告:道具类型失败:提供给“ForwardRef(FormControl)”的“string”类型的道具“error”无效,预期为“boolean” - Warning: Failed prop type: Invalid prop `error` of type `string` supplied to `ForwardRef(FormControl)`, expected `boolean`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM