如果路径更改,如何重新渲染组件?

Dro*_*rop 4 reactjs react-router

如果我在电影页面上

/movie/some-movie
Run Code Online (Sandbox Code Playgroud)

并继续链接到

/movie/another-movie
Run Code Online (Sandbox Code Playgroud)

组件未渲染

反应路线v4

路由器

<Route exact path="/movie/:urlRusLat" component={Movie} />
Run Code Online (Sandbox Code Playgroud)

链接

<Link to={'/' + item.media_type + '/' + urlRusLat(item.title || item.name) + '-' + item.id} className="result-element" key={index}>
Run Code Online (Sandbox Code Playgroud)

我尝试使用componentWillReceiveProps并发送请求,但是我在props的新位置接收消息,例如nextProps。

如果路径更改或重新渲染组件,如何发出发送请求?

小智 6

您可以使用componentDidUpdate()来调用sendRequest()函数。现在,您将获得更新的道具。但是请确保在进行sendRequest()调用之前添加对oldProps.location!= props.location的检查。