gpt4 book ai didi

reactjs - 如果路径改变如何重新渲染组件?

转载 作者:行者123 更新时间:2023-12-03 13:42:16 25 4
gpt4 key购买 nike

如果我在电影页面

/movie/some-movie

然后继续链接到

/movie/another-movie

组件未渲染

react 路线v4

路由器

<Route exact path="/movie/:urlRusLat" component={Movie} />

链接

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

我尝试使用 componentWillReceiveProps 并发送请求,但我在 props 中收到新位置,就像在 nextProps 中一样。

componentWillReceiveProps(nextProps) {
console.log(this.props)
if (this.props.location.pathname !== nextProps.location.pathname) {
this.sendRequest();
}

}

componentDidMount() {
this.sendRequest();
}

componentWillUnmount() {
this.props.clearMovieData();
}

sendRequest = () => {
let movieId = this.props.location.pathname.split('-');
this.props.loadMovieData(movieId[1]);
}

如果路径更改或再次渲染组件,我如何发送请求?

最佳答案

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

关于reactjs - 如果路径改变如何重新渲染组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46895476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com