[英]ReactJS - link to doesn't work for URL with parameter
I use ReactJS and react-router-dom (version 4.2). 我使用ReactJS和react-router-dom(4.2版)。 I can't have my links working with :
我的链接无法使用:
<Link to={'/site/' + site.id}>
<h5><Icon name="map-marker"/> {site.title}</h5>
</Link>
It works when I am on home page ( http://localhost:3000 ), the component is rendered, but when I am on a page like : http://localhost:3000/site/1 , I can't go to URL http://localhost:3000/site/2 . 当我在主页( http:// localhost:3000 )上时,它可以工作,但该组件已呈现,但是当我在http:// localhost:3000 / site / 1这样的页面上时 ,我无法转到URL http:// localhost:3000 / site / 2 。
The URL changes in address bar, but nothing happens. URL在地址栏中更改,但是没有任何反应。
I use : 我用 :
...
<Route path="/site/:id" render={(props) => (
<SitesView id={props.match.params.id} />
)}/>
...
export default connect(mapStateToProps, mapDispatchToProps, undefined, { pure: false })(App);
In my Router. 在我的路由器中。 I can't get it, is it because the URL are almost the same ?
我无法理解,是因为URL几乎相同吗?
As I understood , you have one component <SitesView/>
for all of the /site/
routes with different props.id
's. 据我了解,对于所有
/site/
路由,您只有一个组件<SitesView/>
,具有不同的props.id
。
When you change the parameter of URL, the id of <SitesView/>
component and the URL will be changed but be noticed the <SitesView/>
is rendered on previous route and at the moment you are seeing this rendered component which has no knowledge aboute the changes. 当您更改URL的参数时,
<SitesView/>
组件的ID和URL将被更改,但是会注意到<SitesView/>
在先前的路径上呈现,此刻您看到的是该呈现的组件,它不了解变化。
Then you need to use one of the React LifeCycle Methods named componentWillReceiveProps(nextProp, nextState) to get to know when the id of <SitesView/>
changed . 然后,您需要使用一个名为componentWillReceiveProps(nextProp,nextState)的React LifeCycle方法之一来了解
<SitesView/>
的ID <SitesView/>
更改 。 and based on new id you can update your component. 并根据新ID可以更新组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.