reactjs - How to conditionally route to a component in react based on a variable in the route?
问题描述
Here is an abstract of the what I am trying to imply:
Existing route
<Route path="/public/reports/:reportid/page/:pageid" component={ PageComponent}/>
Idealist expectation of such a behaviour
<Route path="/public/reports/:reportid/page/:pageid" component={ `:pageid` === -1 ? IndexPage : PageCompoennt }/>
To render a pdf in headless browser I have defined a public route, as an addition an index page was added recently, though I have this common route for individual page download, I wanted to reuse the same for index page ( index page doesnt have an identifier, so I thought maybe i'll filter out using -1 to be its index.. The above code is the somewhat the ideal case Im looking for.. Is there a way like this...?
解决方案
您可以使用以下render
道具Route
:
<Route
path="/public/reports/:reportid/page/:pageid"
render={(props) =>
props.match.params.pageid === -1 ?
<IndexPage {...props} />:
<PageCompoennt {...props} />
}
/>
推荐阅读
- javascript - vuejs:嵌套数据更改时更新计算属性
- javascript - 如何将更改存储在 javascript 对象中以便以后使用 Vue 还原它们?
- arrays - 获取多个数组之间的共同元素和一些之间的共同元素
- javascript - 使用 document.getElementsByClassName() 解析网站没有获得标题
- c++ - 符号'是提升正则表达式的特殊符号吗?
- java - 将从 Hibernate Native Query 返回的值映射到模型
- java - Why do i get Exception in thread "main" java.util.NoSuchElementException?
- r - GGPlot Flip This Plot
- python - Groupby Apply Quantile Replacement
- powerbi - PowerBI 中的翻转计算