简体   繁体   English

使用setState时获取React.createElement:类型无效

[英]Getting React.createElement: type is invalid when using setState

在此处输入图片说明

Expected 预期

After clicking the login button without entering in email or password, user should see the Notification component 在未输入电子邮件或密码的情况下单击登录按钮后,用户应看到通知组件

Results 结果

After clicking login, setState is called setting this.state.errors to true, and the above error message is displayed. 单击登录后,将调用setState将此this.state.errors设置为true,并显示以上错误消息。

handleSubmit function handleSubmit函数

Below if I remove the this.setState lines, nothing will happen, but I won't get any Chrome errors. 在下面,如果我删除this.setState行,将不会发生任何事情,但不会出现任何Chrome错误。 However I need that setState so I can then use it to display the Notification (see code below this block) 但是,我需要该setState以便随后可以使用它显示通知(请参见此块下方的代码)

handleLoginSubmit = this.handleLoginSubmit.bind(this);
handleLoginSubmit(e, user) {
  e.preventDefault();
  if (R.isNil(user.email)) return;

  // Log user in via Firebase
  firebase.auth().signInWithEmailAndPassword(user.email, user.password)
    .catch((err) => {
      if (err.code === 'auth/user-not-found') {
        console.log('need to create user')
        return this.createUser(user.email, user.password);
      } else {
        console.log('Incorrect email or password, please try again')

        this.setState({
          errors: true,
          errorMsg: 'Incorrect email or password, please try again'
        }, function () {
          console.log('>>> this.state', this.state);
        });
      }

      console.log('Completed')
    })
}

I also don't see the console.log after setting the state: 设置状态后,我也看不到console.log:

在此处输入图片说明


render() {
  return (
    <main>
      { this.state.errors ? <Notification/> : null }
      <Login handleLoginSubmit={ this.handleLoginSubmit }
             email={ this.state.email }
             password={ this.state.password } />
    </main>
  )
}

Full Code 完整代码

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
import * as R from 'ramda'
import * as firebase from 'firebase'

import { Login } from '../../components'
import { Notification } from '../../components'

export class LoginX extends Component {
    constructor(props) {
        super(props);
        this.state = {
            email: '',
            password: '',
            errors: false,
            errorMsg: ''
        }
        this.handleLoginSubmit = this.handleLoginSubmit.bind(this);
    }

    componentDidMount() {
    firebase.auth().onAuthStateChanged((user) => {
      this.checkAuth();
    })
  }

    componentDidUpdate(prevProps, prevState) {
      console.log('componentDidUpdate this.state', this.state)
    }

    checkAuth() {
    const user = firebase.auth().currentUser;

    if (!user) {
      return
    }
    else {
      if (!user.emailVerified) {
        // User has signed up, redirect them to verification
        this.props.history.push('/verification');
        return
      }
    }

    // User does not need to be authenticated.
    this.props.history.push('/dashboard');
  }

    handleLoginSubmit(e, user) {
        e.preventDefault();
        if (R.isNil(user.email)) return;

        // Log user in via Firebase
        firebase.auth().signInWithEmailAndPassword(user.email, user.password)
      .catch((err) => {
        if (err.code === 'auth/user-not-found') {
          console.log('need to create user')
          return this.createUser(user.email, user.password);
        }
        else {
          console.log('Incorrect email or password, please try again')

          this.setState({
            errors: true,
            errorMsg: 'Incorrect email or password, please try again'
          }, function() {
            console.log('>>> this.state', this.state);
          });
        }

        console.log('Completed')
      })
    }

    createUser(user, pass) {
    firebase.auth().createUserWithEmailAndPassword(user, pass)
      .then((user) => {
        console.log('verification email sent')
        // user.sendEmailVerification()
      })
      .catch((err) => {
        console.log(err)
        // this.setState({inProgress: false})
        switch (err.code) {
          case "auth/email-already-in-use": {
                        console.log('Account already exists, please log in')
            // this.setState({errorMsg: "Account already exists, please log in"});
            break;
          }
          case "auth/invalid-email": {
                        console.log('Invalid email address format (domain is automatically included)')
            // this.setState({errorMsg: "Invalid email address format (domain is automatically included)"});
            break;
          }
          case "auth/operation-not-allowed": {
                        console.log('Login system in unavailable, please contact the system administrator')
            // this.setState({errorMsg: "Login system in unavailable, please contact the system administrator"});
            break;
          }
        }
      })
  }

    render() {
        return (
            <main>
                { this.state.errors ? <Notification/> : null }
                <Login handleLoginSubmit={ this.handleLoginSubmit }
                       email={ this.state.email }
                       password={ this.state.password } />
            </main>
        )
    }
}

const mapStateToProps = (state) => {
    return {
        state
    }
}


const LoginContainer = LoginX;
export default connect(mapStateToProps)(withRouter(LoginContainer))

It's something wrong with Notifications 通知有问题

import React from 'react'
import PropTypes from 'prop-types'

const Notifications = (props) => {

  return (
    <div className="notification">
      Notifications
    </div>
  );
}

export default Notifications

Notifications.propTypes = {

};

在此处输入图片说明

我认为这是一个错字,请使用Notifications而不是Notification

import { Notifications } from '../../components';

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

相关问题 警告:React.createElement:在组件中使用组件时类型无效 - Warning: React.createElement: type is invalid when using a component in a component React:警告:React.createElement:类型无效 - React: Warning: React.createElement: type is invalid React.createElement:在无状态组件中使用array.map时,类型无效 - React.createElement: type is invalid when using array.map inside a stateless component 电子-React.createElement:类型无效错误 - Electron - React.createElement: type is invalid error ReactJS Router-React.createElement:类型无效 - ReactJS Router - React.createElement: type is invalid React.createElement:类型无效(地图) - React.createElement: type is invalid (map) 没有拼写错误,但不断获取React.createElement:类型无效-预期为字符串 - Got No Spelling Error but keep getting React.createElement: type is invalid — expected a string 当我将 React 路由器与 webpack 一起使用时,它显示错误“React.createElement: type is invalid” - When I use react router with webpack, it shows the error "React.createElement: type is invalid" React.createElement:类型无效(redux / react-router?) - React.createElement: type is invalid (redux/react-router?) 警告:React.createElement:类型在React Native Picker中无效 - Warning: React.createElement: type is invalid with React Native Picker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM