React Router v4 - Error: React.Children.only expected to receive one React child

I am using react router v4 in a very simple application. I tried to follow the documentation, but I have a strange error when the application tries to start.

The error says: error: React.Children.only expected to receive a single React element child

The weird thing is that I only have one component in my ReactDOM.render function.

Here are some code snippets:

index.js:

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

      

app.js (I hide all obvious imports):

import { BrowserRouter as Router, Route } from 'react-router-dom'

class App extends Component {
  render() {
    return (
      <Router>
        <div className="App">
          <Route path='/'>
            <Header/>
            <div className="search-container">
              <SearchBar/>
              <AddMovieButton/>
            </div>
            <SearchResultsList/>
          </Route>
          <Route exact path='/new'>
            <AddMovieForm/>
          </Route>
        </div>
      </Router>
    );
  }
}

export default App;

      

I can't figure out why this doesn't work, and why I have this error.

+3


source to share


1 answer


You can only have one child within a route. If you want to have several, wrap them with the div

following.



<Route path="/">
  <div>
    <Header />
    <div className="search-container">
      <SearchBar />
      <AddMovieButton />
    </div>
    <SearchResultsList />
  </div>
</Route>;

      

+5


source







All Articles