尝试得到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;

当前回答

对于我来说,这发生在我试图将命名导入作为默认导入时,所以我得到了这个错误

import ProductCard from '../../../components/ProductCard' // that what caused the issue
Error: Element 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, or you might have mixed up default and named imports.

Check the render method of `TopVente`.

所以我必须通过名称导入来修复它

import { ProductCard } from '../../../components/ProductCard'

其他回答

这意味着您的导入/导出不正确。

检查新增的导入/导出。 在我的例子中,我不必要地使用了大括号。当我去掉这些花括号时,问题就自动解决了。

import { OverlayTrigger } from 'react-bootstrap/OverlayTrigger';

我认为在排除这个错误时最重要的一点是,当您试图实例化一个不存在的组件时,它就会显现出来。这个组件不需要导入。在我的例子中,我将组件作为属性传递。我忘记更新其中一个调用,以便在一些重构之后正确地传递组件。不幸的是,由于JS不是静态类型的,我的错误没有被发现,它花了一些时间来弄清楚发生了什么。

要排除此错误,在呈现组件之前检查该组件,以确保它是您所期望的组件类型。

xxxxx.prototype = {
  dxxxx: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
};

你必须添加// eslint-disable-line react/ forbidden -prop-types,然后它就工作了!

您需要了解命名导出和默认导出。我什么时候应该用大括号导入ES6 ?

在我的例子中,我通过改变

import Provider from 'react-redux'

to

import { Provider } from 'react-redux'

我也得到了这个错误。

我用的是:

从'react-dom'导入

费克斯是这样做的:

import {ReactDOM} from 'react-dom';