最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】ReactJS出错:Uncaught Error A Router may have only one child element

ReactJS crifan 3656浏览 0评论

折腾:

【已解决】React中合并login.html和index.html成单个app.js

期间,代码:

import {
  BrowserRouter as Router,
  Route,
  Link
} from ‘react-router-dom’;
    return (
      <Router>
        <Route path=’/’       component={Login}/>
        <Route path=’/index’  component={Index}/>
      </Router>
    );
  }

结果出错:

browser.js:40 Uncaught Error: A <Router> may have only one child element
    at invariant (browser.js:40)
    at Router.componentWillMount (Router.js:72)
    at eval (ReactCompositeComponent.js:348)
    at measureLifeCyclePerf (ReactCompositeComponent.js:75)
    at ReactCompositeComponentWrapper.performInitialMount (ReactCompositeComponent.js:347)
    at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:258)
    at Object.mountComponent (ReactReconciler.js:46)
    at ReactCompositeComponentWrapper.performInitialMount (ReactCompositeComponent.js:371)
    at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:258)
    at Object.mountComponent (ReactReconciler.js:46)

Uncaught Error A Router may have only one child element

reactjs – React-Router only one child – Stack Overflow

Emphasize routers can only have one child · Issue #4131 · ReactTraining/react-router

React Router: Declarative Routing for React.js

【总结】

然后代码改为:

import {
  BrowserRouter as Router,
  Route,
  Link,
  Switch
} from ‘react-router-dom’;
    return (
      <Router>
        <Switch>
          <Route path=’/’       component={Login}/>
          <Route path=’/index’  component={Index}/>
        </Switch>
      </Router>
    );

就可以了,可以显示页面了:

转载请注明:在路上 » 【已解决】ReactJS出错:Uncaught Error A Router may have only one child element

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.172 seconds, using 22.11MB memory