【问题标题】:Warning: React.createElement: type is invalid -- expected a string (for built-in components)警告:React.createElement:类型无效——需要一个字符串(对于内置组件)
【发布时间】:2018-08-02 03:46:32
【问题描述】:

我需要你的帮助。 警告:React.createElement:类型无效——需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:未定义。您可能忘记从定义组件的文件中导出组件。检查App 的渲染方法。 在 App 中(由 Connect(App) 创建) 在连接(应用程序) 在提供者中

App.js

    import React from 'react';
    import { Router, Route } from 'react-router-dom';
    import { connect } from 'react-redux';
    import { history } from './_helpers/index';
    import { alertActions } from './_actions/index';
    import PrivateRoute from './_components/PrivateRoute.js';
    import Map from './map/Map.js';
    import LoginPage from './LoginPage/LoginPage.js';
    import RegisterPage from './RegisterPage/RegisterPage.js';
    class App extends React.Component {
    constructor(props) {
    super(props);

    const { dispatch } = this.props;
    history.listen((location, action) => {
        // clear alert on location change
        dispatch(alertActions.clear());
    });
  }
render() {
    const { alert } = this.props;
    return (
        <div className="jumbotron">
            <div className="container">
                <div className="col-sm-8 col-sm-offset-2">
                    {alert.message &&
                    <div className={`alert ${alert.type}`}>{alert.message}   </div>
                    }
                    <Router history={history}>
                        <div>
                            <PrivateRoute exact path="/" component={Map} />
                            <Route path="/login" component={LoginPage} />
                            <Route path="/register" component={RegisterPage} />
                        </div>
                    </Router>
                </div>
            </div>
        </div>
    );
    }
    }

    function mapStateToProps(state) {
      const { alert } = state;
     return {
    alert
      };
      }

      export default connect(mapStateToProps)(App);

index.js

  import React from 'react';
  import { render} from 'react-dom';
  import { Provider } from 'react-redux';
  import { store } from './_helpers';
  import App from './App.js';
  import './index.css'; // postCSS import of CSS module
  import { configureFakeBackend } from './_helpers';
  configureFakeBackend();
  render(
      <Provider store={store}>
       <App />
      </Provider>,
document.getElementById('root') );

【问题讨论】:

  • 确认传递给RoutePrivateRoute的组件是各自脚本中的默认导出。
  • 可能是导出默认 PrivateRoute 类的问题
  • 检查 App.js 中所有导入的文件路径是否正确
  • 是的,我确认了所有这些
  • @benlarbiImen 你能提供其他文件吗?

标签: javascript reactjs redux


【解决方案1】:

这是我们所遵循的,它可能会对您有所帮助。

render(&lt;ConfigureRoute store={store} /&gt;, document.getElementByid('root'));

ConfigureRoute 在哪里

const ConfigureRoute = props => (<Provider store={props.store}>
    <Router history={history}>
  <App>
    <Route
      exact
      path="/login"
      render={routeProps => <LoginContainer {...routeProps} {...props} />}
    /></App></Router></Provider>

const App = props => props.children;

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2022-12-12
    • 2018-11-09
    • 2021-11-17
    • 2017-11-29
    • 2020-01-20
    • 2018-01-18
    • 2017-08-06
    • 2019-11-27
    相关资源
    最近更新 更多