当前位置:  开发笔记 > 编程语言 > 正文

未捕获错误:不变违规:元素类型无效:对象

如何解决《未捕获错误:不变违规:元素类型无效:对象》经验,为你挑选了1个好方法。

我正在摆弄react-router,试图实现简单的路由.我在他们的例子中写了我的代码(但没有imports)https://github.com/rackt/react-router#whats-it-look-like.

我在浏览器中收到此错误:

未捕获错误:不变违规:元素类型无效:期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:object.

这就是我的工作.

我在页面上附加脚本,ReactRouter.min.js我的代码在react-routes.js.而且还reactreact-dom和jQuery(所有三个app.js):




这是我的react-router.js,尚未编译:

'use strict';
window.Router = window.ReactRouter;
window.Link = window.ReactRouter.Link;
window.Route = window.ReactRouter.Route;

const Foo = React.createClass({
    render() {
        return (
            

HELLO, me FOO

) } }); ReactDOM.render(( ), document.getElementById('content-section'))

这是我react-router.js与Babel汇编后的.我在页面上正好附上了这个:

babel --presets react -o public/js/react-routes.js assets/js/react-routes.js:

'use strict';

window.Router = window.ReactRouter;
window.Link = window.ReactRouter.Link;
window.Route = window.ReactRouter.Route;

const Foo = React.createClass({
    displayName: "Foo",

    render() {
        return React.createElement(
            "div",
            null,
            React.createElement(
                "h1",
                null,
                "HELLO, me FOO"
            )
        );
    }
});

// Error is thrown here, in this line
ReactDOM.render(React.createElement(
    Router,
    null,
    React.createElement(
        Route,
        { path: "/" },
        React.createElement(Route, { path: "/page/one", component: Foo })
    )
), document.getElementById('content-section'));

我做错了什么?为什么会抛出错误?路由器是一个对象,而不是React组件.为什么?我看一下这个例子并以同样的方式输入我的代码https://github.com/rackt/react-router#whats-it-look-like



1> Michelle Til..:

您的

window.Router = window.ReactRouter;

应该

window.Router = window.ReactRouter.Router;

您正在尝试使用错误,但是它Router是对React Router库(而不是Router组件)的引用.

推荐阅读
云聪京初瑞子_617
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有