ConnectedRouter一个<router>可能只有一个子元素,但有一个子元素

时间:2018-08-05 19:45:14

标签: reactjs redux react-router react-router-redux

所以我有我的App.tsx 我使用了react-router 4中的switch

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的错误还是我只是在这里缺少什么?

1 个答案:

答案 0 :(得分:4)

ConnectedRouter组件内的注释会导致此问题。事实证明,评论也算作孩子。通过将注释移到上方一行来解决此问题:

ReactDOM.render(
  <Provider store={store}>
    { /* place ConnectedRouter under Provider */}
    <ConnectedRouter history={history}>
      <App />
    </ConnectedRouter>
  </Provider>,
  document.getElementById('root') as HTMLElement
)