[英]React component not reloading on history.push
I have an app with routes defined as follows:我有一个应用程序,其路由定义如下:
<Route exact path="/:category/:product" component={ProductComponent} />
On category page, "/:category
I have two buttons for each product, " Quick View " and " Full view ", on click of which I want to move to product page, but also set the state for view like setView('quick')
or setView('full')
.在类别页面上,
"/:category
每个产品都有两个按钮,“快速查看”和“完整查看”,点击它们我想移动到产品页面,但也设置视图状态,如setView('quick')
或setView('full')
。
I tried solving this by using onclick on buttons with the following code:我尝试通过使用 onclick 按钮和以下代码来解决这个问题:
() => {
setView('quick')
history.push(`/${category.slug}/${product.slug}`)
}
here both are called, history api changes the url but component does not load for that url.这里两者都被调用,history api 更改了 url,但组件不会为该 url 加载。 I cannot use the react-router's
<Link>
component because I need to set the view before going to product page.我无法使用 react-router 的
<Link>
组件,因为我需要在进入产品页面之前设置视图。
How can I use the history from react-router to perform page change like <Link>
does?如何使用 react-router 的历史记录来像
<Link>
那样执行页面更改?
Try this尝试这个
Remove component and use render移除组件并使用渲染
<Route
exact
path="/:category/:product"
render={({ match }) => <ProductComponent key={match.params.product || 'empty'} />}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.