尝试得到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 BrowserRouter from 'react-router-dom';

费克斯是这样做的:

import {BrowserRouter} from 'react-router-dom';

其他回答

对于未来的谷歌人:

我解决这个问题的方法是将react和react-dom升级到NPM上的最新版本。显然,我正在导入一个使用新片段语法的组件,它在我的旧版本的React中被打破了。

EDIT

你把过程复杂化了。只要这样做:

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';

ReactDom.render(<routes />, document.getElementById('root'));

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;

简而言之,不知何故发生了以下情况:

render() {
    return (
        <MyComponent /> // MyComponent is undefined.
    );
}

它不一定与某些不正确的输入或输出有关:

render() {
    // MyComponent may be undefined here, for example.
    const MyComponent = this.wizards[this.currentStep];

    return (
        <MyComponent />
    );
}

我的情况不像上面许多人说的那样是一个重要的问题。在我的版本中,我们使用了一个包装器组件来做一些转换逻辑,我错误地传递了子组件,就像这样:

const WrappedComponent = I18nWrapper(<ChildForm {...additionalProps} />);

当我应该把它作为一个函数传递的时候:

const WrappedComponent = I18nWrapper(() => <ChildForm {...additionalProps} />);

这真的很简单。当我开始编写React时,我遇到了这个问题,这个问题几乎总是因为导入:

import React, { memo } from 'react';

你可以对它进行解构,因为react lib有一个memo属性,但你不能这样解构

import { user } from 'assets/images/icons/Profile.svg';

因为它不是一个对象。

希望能有所帮助!