javascript - React 路由器问题未捕获错误 : Invariant Violation: Element type is invalid: expected a string (for built-in components)

标签 javascript reactjs ecmascript-6 webpack react-router

我在使用 react 路由器时遇到一些问题。我收到此错误并且无法呈现我的应用程序。 enter image description here

我到处寻找答案,但找不到任何有用的东西。因此,如果有人可以帮助我,我将非常感激,因为我被困住了。

import React from 'react';
import HomePage from './pages/page.create.jsx';
import { render } from 'react-dom';
import { Router, Route, IndexRoute, Link, IndexLink } from 'react-router';
import { createHistory, useBasename } from 'history';

class CreatePage extends React.Component {
    constructor (props) {
        super(props);
        this.state = {};
    }

    render() {
        return (
            <div className='b-container'>
                <div className = 'test'>
                    <h1>Hello World!</h1>
                </div>
                {this.props.children}
            </div>
        );
    }
}

//render(<HomePage />, document.body);

const routes = {
    path: '/',
    component: CreatePage,
    childRoutes: [
        { path: 'about', component: HomePage }
    ]
};

render(<Router>{routes}</Router>, document.getElementById('root'));

最佳答案

您没有将历史记录传递到路由器。像这样的东西应该可以解决问题:

const history = createHistory();
render(<Router history={history}>{routes}</Router>, document.getElementById('root'))

关于javascript - React 路由器问题未捕获错误 : Invariant Violation: Element type is invalid: expected a string (for built-in components),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33549276/

相关文章:

javascript - 闪存 fscommands 和 javascript

javascript - 如何处理React Native中多个组件的数据获取?

javascript - React 中的 HOC 是否不鼓励这样做?

javascript - 在 Javascript 中允许命名参数或位置参数

javascript - 正确使用表单和输入元素

javascript - 将 JSON 对象映射到单独的 div 中

javascript - Jquery - 删除最后一个附加项

javascript - 在关闭页面或单击后退按钮之前提示用户验证的消息

reactjs - react 钩子(Hook)形式 V7 : value is not cleared after unmounting its registered component

javascript - 在 ES6 中使用递归 reduce 转换所有子对象