简体   繁体   English

如何使用 react router dom 在另一个组件中呈现一个组件中存在的链接?

[英]How do you render Links present in one component in another component using react router dom?

Let's say we have a main component, and a navbar and a sidebar component now I have some links to movies, games and tv shows etc. How do you render movies component in the main component itself upon clicking the links in the sidebar假设我们有一个主要组件、一个导航栏和一个侧边栏组件,现在我有一些电影、游戏和电视节目等的链接。单击侧栏中的链接时,如何在主要组件中呈现电影组件

app.js code app.js 代码

//imports here

<Router>
   <Route exact path='/login' component={Login} />
   <Route path='/admin' component={Main} />
</Router>

sidebar.js侧边栏.js

//imports here
//styling
<ul>
<Link to='/movies'> <li>Movies </li> </Link>
<Link to='/games'> <li>games</li> </Link>
<Link to='/tv-shows'> <li>Tv shows</li> </Link>
</ul>

main.js主文件

//imports here
<div>
  <Navbar/>
  <div>
  <Sidebar />
    <Switch>
      <Route path='/movies' component={Movies} />
      <Route path='/games' component={Games} />
      <Route path='/tv-shows' component={TvShows} />
    </Switch>
 </div>
</div>

The problem with above approach is URL is getting changed but I'm not getting the component, if I include the routes ie movies or games etc in app.js then sidebar and navbar are not getting rendered.上述方法的问题是 URL 正在更改,但我没有获取组件,如果我在 app.js 中包含路由,即电影或游戏等,则侧边栏和导航栏不会被呈现。 My question how do you render the movies component or games component in the Main.js itself so that I get sidebar and navbar as well as the given component?我的问题是如何在 Main.js 本身中渲染电影组件或游戏组件,以便我获得侧边栏和导航栏以及给定的组件? Any guidance?任何指导?

Since movies, games are child components you can try changing the paths as follows:由于电影、游戏是子组件,您可以尝试按如下方式更改路径:

<Link to='/admin/movies'> <li>Movies </li> </Link>

and

<Route path='/admin/movies' component={Movies} />

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

相关问题 当使用Route render prop而不是Route component prop时,如何访问react-router-dom 4中的url args? - How do you access url args in react-router-dom 4 when using Route render prop instead of Route component prop? 如何添加将使用 react-router-dom 渲染组件的路由? - How to add a route that will render a component using react-router-dom? React Router Dom不渲染组件 - React router dom not render component React-router-dom,路由组件不渲染 - React-router-dom, route component not render React-Router-Dom 6 - 如何动态渲染组件? - React-Router-Dom 6 - How to dynamically render a component? react-router-dom:如何进行异步调用来决定渲染组件? - react-router-dom : How to do async calls to decide to render a component? 如何使用路由器/路由器参数将数据从一个父组件传输到另一个父组件 - How to transfer data from one parent component to another parent component in react using router / router params 你如何将 React 组件渲染为字符串 - How do you Render a React Component to a String 如何使用react-router-dom定向链接以呈现填充有prop的常规post组件? - How, using react-router-dom, can I direct a link to render a general post component, populated with props? React Router,如何告诉路由器渲染到组件? - React Router, how do I tell the router to render to a component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM