react router问题未捕获错误:不变冲突:元素类型无效:应为字符串(用于内置组件)

Issue with react router Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components)

本文关键字:无效 字符串 组件 内置 用于 类型 冲突 问题 router 错误 react      更新时间:2023-09-26

我在使用react路由器时遇到了一些问题。我收到这个错误,无法呈现我的应用程序。在这里输入图像描述

我到处找答案,但找不到任何有用的答案。所以,如果有人能帮助我,我将非常感激,因为我陷入了困境。

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'))

谢谢大家。问题出现在react路由器版本中,我尝试使用v1.0的功能,但安装的是v13.5。为了下定决心,我重新安装了路由器,一切都开始了。