React-Router只有一个孩子

时间:2017-03-24 06:33:08

标签: javascript reactjs react-router

我一直在收到错误:

  

A'路由器'可能只有一个子元素

使用react-router时。

我似乎无法弄清楚为什么这不起作用,因为它与他们在示例中显示的代码完全相同:https://reacttraining.com/react-router/web/guides/quick-start

这是我的代码:

import React from 'react';
import Editorstore from './Editorstore';
import App from './components/editor/App';
import BaseLayer from './components/baselayer';
import {BrowserRouter as Router, Route} from 'react-router-dom';
import {render} from 'react-dom';

const root = document.createElement('div');
root.id = 'app';
document.body.appendChild(root);

const store = new Editorstore();
const stylelist = ['https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.2/semantic.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css', 'https://api.tiles.mapbox.com/mapbox-gl-js/v0.33.1/mapbox-gl.css'];

stylelist.map((link) => {
    const a = document.createElement('link');
    a.rel = 'stylesheet';
    a.href = link;
    document.body.appendChild(a);
    return null;
});

render((
  <Router>
    <Route exact  path="/" component={BaseLayer} />
    <Route path="/editor" component={App} store={store} />
  </Router>
), document.querySelector('#app'));

感谢您的帮助

10 个答案:

答案 0 :(得分:135)

您必须将Route包裹在<div>(或<Switch>)中。

render((
  <Router>
    <Route exact  path="/" component={BaseLayer} />
    <Route path="/editor" component={App} store={store} />
  </Router>
), document.querySelector('#app'));

应该是

render((
  <Router>
    <div>
       <Route exact  path="/" component={BaseLayer} />
       <Route path="/editor" component={App} store={store} />
    </div>
  </Router>
), document.querySelector('#app'));

jsfiddle / webpackbin

答案 1 :(得分:35)

这是react-router 4.x中的API更改。建议的方法是将Route包裹在Switchhttps://github.com/ReactTraining/react-router/issues/4131#issuecomment-274171357

引用:

转换

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

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

当然,您需要在导入中添加Switch

import { Switch, Router, Route } from 'react-router'

答案 2 :(得分:3)

我总是在反应网和原生(&gt; =反应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

答案 3 :(得分:2)

我将所有<Route />标记都放在<Switch> </Switch>标记内。

    <BrowserRouter>
        <Switch>
            <Route path='/' component={App} exact={true} /> 
            <Route path='/form-example' component={FormExample} />
        </Switch>
    </BrowserRouter>

这解决了这个问题。

答案 4 :(得分:1)

如果要在路由器内部嵌套其他组件,则应该这样做。

  <Router>
     <div>
       <otherComponent/>
         <div>
           <Route/>  
           <Route/>
           <Route/>
           <Route/>
         </div>
      </div>
    </Router>

答案 5 :(得分:1)

如果您使用的是Reach Routers,请确保代码如下所示:

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

在React Router的情况下,将这些组件包括在Div中将使此工作有效,但在Reach Router中,请删除该Div元素。

答案 6 :(得分:0)

您还可以将所有路线包裹在默认为索引页的父路线中

<Router history={history}>    
   <Route path="/" component={IndexPage}>
      <Route path="to/page" component={MyPage}/>
      <Route path="to/page/:pathParam" component={MyPage}/>
   </Route>    
</Router>

答案 7 :(得分:0)

我正在使用react-router-dom版的5.0.1软件包,它与您的代码配合得很好。

import { BrowserRouter as Router , Router, Link } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';
...

class App extends React.Component {
  render() {
    return (
      <Router>
        <ul>
          <li><Link path='/'>Home</Link></li>
          <li><Link path='/about'>About</Link></li>
        </ul>
        <Route path='/' exact component={Home} />
        <Route path='/about' component={About} />
      </Router>
    );
  }
}

export default App;

答案 8 :(得分:0)

不确定我的路由器是否太简单,或者对此规则进行了更改,但是遵循了提到此限制的教程(“路由器”可能只有一个子元素)它使我可以添加3条路线而不会出现任何错误。这是工作代码:

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

这是我的依赖项:

"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",

答案 9 :(得分:-1)

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>