ConnectedRouter A <Router> 可能只有一个子元素,但它有一个子元素
ConnectedRouter A <Router> may have only one child element but it has one child
所以我有我的 App.tsx
我使用来自 react-router 4
的开关
import { Rate } from 'antd'
import * as React from 'react'
import './App.scss'
import { Route, Switch } from 'react-router' // react-router v4
class App extends React.Component {
public render() {
return (
<div className="App">
<div> { /* your usual react-router v4 routing */}
<Switch>
<Route exact path="/" render={() => (<div>Match</div>)} />
<Route render={() => (<div>Miss</div>)} />
<Rate />
</Switch>
</div>
</div>
)
}
}
export default App
我有 index.tsx
这样的
这里我使用connected-react-router连接redux。
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import App from './App'
import './index.scss'
import registerServiceWorker from './registerServiceWorker'
import store, { history } from './store'
import { Provider } from 'react-redux'
import { ConnectedRouter } from 'connected-react-router'
ReactDOM.render(
<Provider store={store}>
<ConnectedRouter history={history}> { /* place ConnectedRouter under Provider */}
<App />
</ConnectedRouter>
</Provider>,
document.getElementById('root') as HTMLElement
)
registerServiceWorker()
如您所见,路由器确实只有一个元素,但我收到一条错误消息,指出路由器应该只有一个子元素。这是 connected-react-router 的错误还是我只是在这里遗漏了什么?
ConnectedRouter
组件中的注释导致了问题。事实证明,评论也算作 children。通过将注释移到上面一行来修复它:
ReactDOM.render(
<Provider store={store}>
{ /* place ConnectedRouter under Provider */}
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
</Provider>,
document.getElementById('root') as HTMLElement
)
所以我有我的 App.tsx
我使用来自 react-router 4
import { Rate } from 'antd'
import * as React from 'react'
import './App.scss'
import { Route, Switch } from 'react-router' // react-router v4
class App extends React.Component {
public render() {
return (
<div className="App">
<div> { /* your usual react-router v4 routing */}
<Switch>
<Route exact path="/" render={() => (<div>Match</div>)} />
<Route render={() => (<div>Miss</div>)} />
<Rate />
</Switch>
</div>
</div>
)
}
}
export default App
我有 index.tsx
这样的
这里我使用connected-react-router连接redux。
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import App from './App'
import './index.scss'
import registerServiceWorker from './registerServiceWorker'
import store, { history } from './store'
import { Provider } from 'react-redux'
import { ConnectedRouter } from 'connected-react-router'
ReactDOM.render(
<Provider store={store}>
<ConnectedRouter history={history}> { /* place ConnectedRouter under Provider */}
<App />
</ConnectedRouter>
</Provider>,
document.getElementById('root') as HTMLElement
)
registerServiceWorker()
如您所见,路由器确实只有一个元素,但我收到一条错误消息,指出路由器应该只有一个子元素。这是 connected-react-router 的错误还是我只是在这里遗漏了什么?
ConnectedRouter
组件中的注释导致了问题。事实证明,评论也算作 children。通过将注释移到上面一行来修复它:
ReactDOM.render(
<Provider store={store}>
{ /* place ConnectedRouter under Provider */}
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
</Provider>,
document.getElementById('root') as HTMLElement
)