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

当前回答

在我的例子中,创建组件和渲染的顺序很重要。我在创建组件之前渲染了它。最好的方法是创建子组件,然后是父组件,然后呈现父组件。改变顺序为我解决了这个问题。

其他回答

如果在测试组件时出现此错误,请确保每个子组件在单独运行时都能正确呈现,如果其中一个子组件依赖于外部资源来呈现,请尝试使用jest或任何其他mock库来模拟它:

示例:

jest.mock('pathToChildComponent', () => 'mock-child-component')

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

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} />);

你可能会从某个文件返回object而不是只返回语句

错误的:

import React from 'react'
const About = () => {   return ( <div>About</div>   ) }
export default About

正确的:

import React from 'react'
const About = () => {   return 
<div>About</div>} 
export default About

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

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'