我在使用 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/