React-Router only one child

后端 未结 10 1730
执笔经年
执笔经年 2020-12-07 14:32

I keep on getting the error:

A \'Router\' may have only one child element

when using react-router.

I can\'t seem to fig

相关标签:
10条回答
  • 2020-12-07 15:02

    If you are nesting other components inside the Router you should do like.

      <Router>
         <div>
           <otherComponent/>
             <div>
               <Route/>  
               <Route/>
               <Route/>
               <Route/>
             </div>
          </div>
        </Router>
    
    0 讨论(0)
  • 2020-12-07 15:03

    Not sure if my router might be too simple, or there was a change to this rule but was following along a tutorial that mentioned this limitation (A 'Router' may have only one child element) and it allowed me to add 3 routes without giving any errors. This is the working code:

    function render() {
      ReactDOM.render(
        <BrowserRouter>
          <Route exact path="/" component={App} />
          <Route path="/add" component={AddAuthorForm} />
          <Route path="/test" component={test} />
        </BrowserRouter>
        ,
        document.getElementById('root')
      );
    }
    

    And this are my dependencies:

    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.4.1",
    
    0 讨论(0)
  • 2020-12-07 15:10

    This problem occurs when you don't have parent tag before <Route>inside <Router> so to resolve this problem keep the <Route>enclosed in a parent tag such as <div> , <p> etc. Example -

    <Router>
        <p>
           <Route path="/login" component={Login} />
           <Route path="/register" component={Register} />
        </p>
    </Router>
    
    0 讨论(0)
  • 2020-12-07 15:13

    If you are using Reach Routers make sure the Code looks like this:

    <Router>
        <Login path="/" />
        <Login path="/login" />
    </Router>
    

    Including these Components in a Div in the case of React Routers will make this work but In Reach Routers, Remove that Div Element.

    0 讨论(0)
  • 2020-12-07 15:15

    This is an API change in react-router 4.x. Recommended approach is to wrap Routes in a Switch: https://github.com/ReactTraining/react-router/issues/4131#issuecomment-274171357

    Quoting:

    Convert

    <Router>
      <Route ...>
      <Route ...>
    </Router>
    

    to

    <Router>
      <Switch>
        <Route ...>
        <Route ...>
      </Switch>
    </Router>
    

    You will, of course, need to add Switch to your imports:

    import { Switch, Router, Route } from 'react-router'
    
    0 讨论(0)
  • 2020-12-07 15:15

    I Always use Fragment in react web and native ( >= react 16 )

    import React, { Component, Fragment } from 'react'
    import { NativeRouter as Routes, Route, Link } from 'react-router-native'
    import Navigation from './components/navigation'    
    import HomeScreen from './screens/home'
    import { RecipesScreen } from './screens/recipe'
    
    class Main extends Component {
      render() {
        return (
          <Fragment>
            <Navigation />
            <Routes>
              <Fragment>
                <Route exact path="/" component={HomeScreen} />
                <Route path="/recipes" component={RecipesScreen} />
              </Fragment>
            </Routes>
          </Fragment>
        )
      }
    }
    
    export default Main
    
    0 讨论(0)
提交回复
热议问题