【发布时间】:2020-02-26 14:56:43
【问题描述】:
为简单起见,详细信息页面根据 URL 中的电影 ID 获取装载数据,这来自路径中的 path='movie/:id'。
它的孩子被称为推荐,它再次根据当前 URL 向您显示推荐的电影。
class MovieDetailPage extends React.Component {
// Fetch movies and cast based on the ID in the url
componentDidMount() {
this.props.getMovieDetails(this.props.match.params.id)
this.props.getMovieCast(this.props.match.params.id)
}
render() {
<div>
Movies here
</div>
<Recommended id={this.props.match.params.id}/>
}
}
Recommended 组件也会根据当前电影获取数据,并生成另一个指向另一部电影的标签。
class Recommended extends React.Component {
componentDidMount() {
this.props.getRecommended(this.props.id)
}
render() {
return (
<>
<Category title={'Recommended'}></Category>
<div className="movies">
{
this.props.recommended.map((movie) => {
return (
<Link key={movie.id} to={`movie/${movie.id}`} className="movies__item">
<img
key={movie.id}
src={`https://image.tmdb.org/t/p/w342${movie.poster_path}`}
className="movies__item-img"
alt={`A poster of ${movie.title}`}
>
</img>
</Link>
)
})
}
</div>
</>
)
}
}
现在如何在点击推荐组件中生成的链接时触发父组件的另一个渲染? URL 正在更改,但这不会像我打算那样触发渲染。
更新:
<Route
path="/movie/:id"
render={(props) => (
<MovieDetailPage key={props.match.params.id}
{...props}
)}
/>
这次我传入了一个触发页面重新呈现的唯一键。我以前试过这个,但我可能搞砸了语法。
这篇文章让我找到了正确的方向:Force remount component when click on the same react router Link multiple times
【问题讨论】:
标签: javascript reactjs