问题描述
尝试让 react-router (v4.0.0) 和 react-hot-loader (3.0.0-beta.6) 运行良好,但在浏览器控制台中出现以下错误:
Trying to get react-router (v4.0.0) and react-hot-loader (3.0.0-beta.6) to play nicely, but getting the following error in the browser console:
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;
推荐答案
大多数情况下这是由于导出/导入不正确造成的.
Most of the time this is due to an incorrect export/import.
常见错误:
// File: LeComponent.js
export class LeComponent extends React.Component { ... }
// File: App.js
import LeComponent from './LeComponent';
可能的选择:
// File: LeComponent.js
export default class LeComponent extends React.Component { ... }
// File: App.js
import LeComponent from './LeComponent';
有几种方法可能会出错,但该错误是由于每次 60% 的导入/导出不匹配造成的.
There are a few ways it could be wrong, but that error is because of an import/export mismatch 60% of the time, everytime.
编辑
通常,您应该获得一个堆栈跟踪,指示故障发生的大致位置.这通常紧跟在您在原始问题中收到的消息之后.
Typically you should get a stacktrace that indicates an approximate location of where the failure occurs. This generally follows straight after the message you have in your original question.
如果它没有显示,则可能值得调查原因(这可能是您缺少的构建设置).无论如何,如果它没有显示,唯一的做法就是缩小导出/导入失败的位置.
If it doesn't show, it might be worth investigating why (it might be a build setting that you're missing). Regardless, if it doesn't show, the only course of action is narrowing down where the export/import is failing.
遗憾的是,没有堆栈跟踪的唯一方法是手动删除每个模块/子模块,直到您不再收到错误为止,然后按自己的方式返回堆栈.
Sadly, the only way to do it, without a stacktrace is to manually remove each module/submodule until you don't get the error anymore, then work your way back up the stack.
编辑 2
通过评论,确实是导入问题,特别是导入了一个不存在的模块
Via comments, it was indeed an import issue, specifically importing a module that didn't exist
这篇关于React.createElement:类型无效——需要一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!