Search code examples
reactjsreduxreact-routerreact-router-redux

ConnectedRouter A <Router> may have only one child element but it has one child


So I have my App.tsx I use switch from 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

And I have index.tsx like this Here I use connected-react-router to connect to 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()

As you can see Router indeed has just one element however I get an error that the router should have only one child. Is this a bug of connected-react-router or am I just missing something in here?


Solution

  • The comment inside the ConnectedRouter component causes the problem. As it turns out, comments also count as children. Fix it by moving the comment one line above:

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