React.createElement:类型无效-应该是一个字符串 [英] React.createElement: type is invalid -- expected a string

查看:414
本文介绍了React.createElement:类型无效-应该是一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图使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 getitng the following error in the browser console:

警告:React.createElement:类型无效-预期为字符串 (对于内置组件)或类/函数(对于复合 组件),但得到:未定义.您可能忘记了导出 定义文件中的组件.

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:

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:类型无效-应该是一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆