reactjs – React.createElement:type无效 – 期望一个字符串

试图让react-router(v4.0.0)和react-hot-loader(3.0.0-beta.6)很好地播放,但是在浏览器控制台中得到以下错误

Warning: React.createElement: type is invalid — expected a string
(for built-in components) or a class/function (for composite
components) but got: undefined. You likely forgot to export your
component from the file it’s defined in.

index.js:

import React from 'react';
import ReactDom from 'react-dom';
import routes from './routes.js';
require('jquery');
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap/dist/js/bootstrap.min.js';
import './css/main.css';

const renderApp = (appRoutes) => {
    ReactDom.render(appRoutes,document.getElementById('root'));
};

renderApp( routes() );

routes.js:

import React from 'react';
import { AppContainer } from 'react-hot-loader';
import { Router,Route,browserHistory,IndexRoute } from 'react-router';
import store from './store/store.js';
import { Provider } from 'react-redux';
import App from './containers/App.jsx';
import Products from './containers/shop/Products.jsx';
import Basket from './containers/shop/Basket.jsx';

const routes = () => (

    <AppContainer>
        <Provider store={store}>
            <Router history={browserHistory}>
                <Route path="/" component={App}>
                    <IndexRoute component={Products} />
                    <Route path="/basket" component={Basket} />
                </Route>
            </Router>
        </Provider>
    </AppContainer>

);

export default routes;
大多数情况下,这是因为您没有正确导出/导入。

常见错误

// File: LeComponent.js
export class LeComponent extends React.Component { ... }

// File: App.js
import LeComponent from './LeComponent';

// no "default" export,should be  export default class LeComponent

有几种方法可能是错误的,但这种错误是由于每次都有60%的时间导入/导出错误

编辑

通常,您应该获得一个堆栈跟踪,指示发生故障的位置的大致位置。这通常紧跟在您原始问题中的消息之后。

如果它没有显示,可能值得调查原因(它可能是您缺少的构建设置)。无论如何,如果它没有显示,唯一的行动方针是缩小导出/导入失败的地方。

遗憾的是,没有堆栈跟踪的唯一方法是手动删除每个模块/子模块,直到你不再得到错误,然后按照你的方式备份堆栈。

编辑2

通过评论,这确实是一个导入问题,特别是导入一个不存在的模块

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...