错误:元素类型无效:预期为字符串或类/函数,但得到了:对象

时间:2020-04-06 10:02:17

标签: javascript reactjs

我收到此错误:

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

检查Register的呈现方法。

这是我的注册码:

import React from 'react';
import Button from '../../../components/atoms/Button'; 
import { connect } from 'react-redux';
import { registerUserAPI } from '../../../config/redux/action';

class Register extends React.Component {
  state = {
    email: '',
    password: '',  
  }

  handleChangeText = (e) => {
    this.setState({
      [e.target.id]: e.target.value
    })
  }

  handleRegisterSubmit = async () => {
    const {email, password} = this.state;
    const res = await this.props.registerAPI({email, password}).catch(err => err);
    if (res) {
      this.setState({
        email: '',
        password: ''
      }) 
    }  
  }
  render() {
    return (
      <div className="auth-container"> 
      <div className="auth-card">
        <p className="auth-title">Register Page</p>
        <input className="input" id="email" placeholder="Email" type="text" onChange={this.handleChangeText} value={this.state.email} />
        <input className="input" id="password" placeholder="Password" type="password" onChange={this.handleChangeText} value={this.state.password} />
        <Button onClick={this.handleRegisterSubmit} title="Register" loading={this.props.isLoading} />
      </div> 
      </div>
    );
  }
}

const reduxState = (state) => ({
  isLoading: state.isLoading
})

const reduxDispatch = (dispatch) => ({
  registerAPI: (data) => dispatch(registerUserAPI(data));
})

export default connect(reduxState, reduxDispatch)(Register);

应用

import React from 'react';
import ReactDOM from 'react-dom';
import TopupPage from '../TopupPage';
import Register from '../Register';
import Dashboard from '../Dashboard/Layout';
import Login from '../Login';
import Homepage from '../Homepage';
import {
  BrowserRouter as Router,
  Route,
  Link
} from 'react-router-dom';
import { Provider } from 'react-redux';
import { store } from '../../../config/redux';
import thunk from 'redux-thunk';
import { createStore, applyMiddleware } from 'redux';

function App(){
  return (
    <Provider store={store}>
      <Router>
        <div>
          <Route path="/" exact component={Homepage} />
          <Route path="/register" component={Register} />
          <Route path="/TopupPage" component={TopupPage} />
          <Route path="/login" component={Login} />
        </div>
      </Router>
   </Provider>
 );
}

export default App;

2 个答案:

答案 0 :(得分:2)

更改:

  <Route path="/login" component={<Login/>} />

  <Route path="/login" component={Login} />

这也是语法错误:

const reduxDispatch = (dispatch) => ({
  registerAPI: (data) => dispatch(registerUserAPI(data));
})

您应该删除“;”。

答案 1 :(得分:0)

正如错误所述,您正在将一些对象传递到应具有原始值的位置。

我最好的办法是,在reduxState中,您应该有类似这样的内容:

const reduxState = (state) => ({
        isLoading: state.someKeyWhichYouAreMissing.isLoading
    })

调试的最佳方法是将整个渲染替换为:

render() {
  console.log(this.state.email, this.state.password, this.props.isLoading)
  return null;
}