简体   繁体   English

React Router v5.0.1-在同一路径上渲染两个组件

[英]React router v5.0.1 - Rendering two components on same route

I have set two separate routes , but both the routes are being rendered for localhost:3000 我设置了两个单独的路由,但是两个路由都针对localhost:3000进行渲染

I have looked at stackoverflow questions regarding the same issue but not resolving my issue . 我看过关于同一问题的stackoverflow问题,但没有解决我的问题。

I have wrapped my routes in switch , tried alternating the order of routes , used exact and have also forced id to look for only numeric . 我已经将我的路由包装在switch中,尝试更改路由的顺序,使用了精确的路由,并且还强制id只寻找数字。

Have referred to - React router v4 - Rendering two components on same route 参考-React router v4-在同一路径上渲染两个组件

but not able to resolve my issue 但无法解决我的问题

import { BrowserRouter as Router, Switch, Route } from "react-router-dom";

render() {
    return (
      <Router>
        <Switch>
         <Route exact path="/" render={props => <Table stu={this.state} />} />
          <Route
            path={`/:id(\\d+)`}
            render={props => <Detail data={this.state} />}
          />
        </Switch>
      </Router>
    );
  }

Both Table and Detail components are being rendered for localhost:3000 whereas I want only Table component to be rendered for localhost:3000. Table和Detail组件都为localhost:3000呈现,而我只希望Table组件为localhost:3000呈现。

正如Ilya kovalyov在上面的评论中指出的那样-在我的Table组件的render方法中,我将道具传递给Detail组件,而Detail也在被渲染

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM