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

当前回答

在我的情况下,我只需要从react-router-redux升级到react-router-redux@next。我猜肯定是兼容性问题。

其他回答

我也得到了这个错误。

我用的是:

import BrowserRouter from 'react-router-dom';

费克斯是这样做的:

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

对于未来的谷歌人:

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

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

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'

这是一个在某种程度上必须调试的错误。正如已经说过很多次,不恰当的导入/导出会导致这个错误,但令人惊讶的是,我从我的react-router-dom认证设置中的一个小错误中得到了这个错误,下面是我的情况:

错误的设置:

const PrivateRoute = ({ component: Component, ...rest }) => (
    <Route
        {...rest}
        render={(props) => (token ? <Component {...props} /> : <Redirect to={{ pathname: "/login" }} />)}
    />
);

正确设置:

const PrivateRoute = ({ component: Component, token, ...rest }) => (
    <Route
        {...rest}
        render={(props) => (token ? <Component {...props} /> : <Redirect to={{ pathname: "/login" }} />)}
    />
);

唯一的区别是我在PrivateRoute组件中解构了令牌。顺便说一下,令牌是从localstorage获取的,像这样const token = localstorage . getitem ("authUser");所以如果它不在那里,我知道用户没有验证。这也会导致错误。

我丢失了一个React片段:


function Bar({ children }) {

  return (
    <div>
     {children}
    </div>
  );
}

function Foo() {
  return (
    <Bar>
      <Baz/>
      <Qux/>
    </Bar>
  );
}

上面的代码抛出上面的错误。但这就解决了问题:

<Bar>
  <>
    <Baz/>
    <Qux/>
  </>
</Bar>