错误“'元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到了:对象。”

时间:2019-04-07 18:25:22

标签: reactjs firebase

我正在使用此tutorial学习React。现在我要处理SignUp部分。我被困在这里:  应该在SignUp的index.js中,但已“移动”到Firebase中的context.js中。之后,在运行脚本时收到如下错误消息:

  

元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到了:对象。您可能忘记了从定义文件中导出组件,或者可能混淆了默认导入和命名导入。

     

检查Context.Consumer的呈现方法。

似乎错误也与src中的index.js有关:

  

./ src / index.js

     

src / index.js:10

   7 | import App from './components/App';
   8 | import Firebase, { FirebaseContext } from './components/Firebase';
   9 | 
> 10 | ReactDOM.render(
  11 |   <FirebaseContext.Provider value={new Firebase()}>
  12 |     <App />
  13 |   </FirebaseContext.Provider>,

我很困惑,因为我认为FirebaseContext.Provider是在src / index.js中定义的,并且FirebaseContext.Consumer已经移到Firebase的context.js吗?这里有什么问题以及如何解决?

谢谢。

这是SignUp的index.js:

import React, { Component } from 'react';
import { Link, withRouter } from 'react-router-dom';
import { withFirebase } from '../Firebase';
import * as ROUTES from '../../constants/routes';

const SignUpPage = () => (
  <div>
    <h1>SignUp</h1>
      <SignUpForm />
      //<SignUpForm />
  </div>
);

const INITIAL_STATE = {
  username: '',
  email: '',
  passwordOne: '',
  passwordTwo: '',
  error: null,
};

class SignUpFormBase extends Component {
  constructor(props) {
    super(props);
  }

  onSubmit = event => {
    const { username, email, passwordOne } = this.state;

    this.props.firebase
      .doCreateUserWithEmailAndPassword(email, passwordOne)
      .then(authUser => {
        this.setState({ ...INITIAL_STATE });
        this.props.history.push(ROUTES.HOME);
      })
      .catch(error => {
        this.setState({ error });
      });

    event.preventDefault();
  }

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

  render() {
    const {
      username,
      email,
      passwordOne,
      passwordTwo,
      error,
    } = this.state;

    const isInvalid =
          passwordOne !== passwordTwo ||
          passwordOne === '' ||
          email === '' ||
          username === '';

    return (
      <form onSubmit={this.onSubmit}>
        <input
          name="username"
          value={username}
          onChange={this.onChange}
          type="text"
          placeholder="Full Name"
        />
        <input
          name="email"
          value={email}
          onChange={this.onChange}
          type="text"
          placeholder="Email Address"
        />
        <input
          name="passwordOne"
          value={passwordOne}
          onChange={this.onChange}
          type="password"
          placeholder="Password"
        />
        <input
          name="passwordTwo"
          value={passwordTwo}
          onChange={this.onChange}
          type="password"
          placeholder="Confirm Password"
        />
        <button disabled={isInvalid} type="submit">Sign Up</button>

        {error && <p>{error.message}</p>}
      </form>
    );
  }
}

const SignUpLink = () => (
  <p>
    Don't have an account? <Link to={ROUTES.SIGN_UP}>Sign Up</Link>
  </p>
);

const SignUpForm = withFirebase(SignUpFormBase);

export default SignUpPage;

export { SignUpForm, SignUpLink };

这是index.js代码:

import React from 'react';
import ReactDOM from 'react-dom';

import './index.css';
import * as serviceWorker from './serviceWorker';

import App from './components/App';
import Firebase, { FirebaseContext } from './components/Firebase';

ReactDOM.render(
  <FirebaseContext.Provider value={new Firebase()}>
    <App />
  </FirebaseContext.Provider>,
  document.getElementById('root'),
);

serviceWorker.unregister();

0 个答案:

没有答案