英文:
How can I navigate to a new page based on the image selected that was pulled form an API in react?
问题
I have a personal movie database website I'm working on with react.js. The homepage displays a main recommended random movie, and then specific rows of movies. These movies are all generated by using an API from TMDB.org.
What I would like to accomplish is the ability to click on an image (or the main recommended movie) and be redirected to a page specific to the movie requested with the attributing data like photos, release date, etc.
I assume I need to do something like a getElementByID function, but I'm not sure how to do that. Also how would I link that in my app.js. (Right now I have a MovieDetails.jsx page which can be accessed by going to the url/moviedetails page. I assume it will use that page and alter it every time you go to it with a new movie.)
This is the code that pulls in the images from the specific movie titles.
<div className="w-[160px] sm:w-[200px] md:w-[240px] lg:w-[280px] inline-block cursor-pointer relative p-2">
<img
className="w-full h-auto block"
src={`https://image.tmdb.org/t/p/w500/${item?.backdrop_path}`}
alt={item?.title}
/>
<div className="absolute top-0 left-0 w-full h-full hover:bg-black/80 opacity-0 hover:opacity-100 text-white">
<p className="white-space-normal text-xs md:text-sm font-bold flex justify-center items-center h-full text-center">
{item?.title}
</p>
</div>
</div>
英文:
I have a personal movie database website I'm working on with react.js. The homepage displays a main recommended random movie, and then specific rows of movies. These movies are all generated by using an API from TMDB.org.
What I would like to accomplish is the ability to click on an image (or the main recommended movie) and be redirected to a page specific to the movie requested with the attributing data like photos, release date, etc.
I assume I need to do something like a getElementByID function, but I'm not sure how to do that. Also how would I link that in my app.js. (Right now I have a MovieDetails.jsx page which can be accessed by going to the url/moviedetails page. I assume it will use that page and alter it every time you go to it with a new movie.)
This is the code that pulls in the images from the specific movie titles.
<div className="w-[160px] sm:w-[200px] md:w-[240px] lg:w-[280px] inline-block cursor-pointer relative p-2">
<img
className="w-full h-auto block"
src={`https://image.tmdb.org/t/p/w500/${item?.backdrop_path}`}
alt={item?.title}
/>
<div className="absolute top-0 left-0 w-full h-full hover:bg-black/80 opacity-0 hover:opacity-100 text-white">
<p className="white-space-normal text-xs md:text-sm font-bold flex justify-center items-center h-full text-center">
{item?.title}
</p>
</div>
</div>
答案1
得分: 0
以下是翻译好的部分:
1- 在你的 App.js
中:
<BrowserRouter>
<Route path="movie/:id" element={<Movie />} />
</BrowserRouter>
2- 将 Link
添加到你的图像:
<Link to={`movie/${item?.id}`}>
<img
className="w-full h-auto block"
src={`https://image.tmdb.org/t/p/w500/${item?.backdrop_path}`}
alt={item?.title}
/>
</Link>
这个想法是根据这个 id 传递给动态页面。确保你的数据中有 id
属性。
3- 创建一个新的组件 Movie.js
:
const Movie = () => {
let { id } = useParams();
// 根据 "id" 检索数据或进行筛选
return (
// 在这里添加你的代码
)
}
当然,不要忘记导入 BrowseRouter
、Route
、Link
、Movie
和 useParams
。
希望这对你清楚明了。
英文:
You can use dynamics pages to deal with it
1- In your App.js
:
<BrowserRouter>
<Route path="movie/:id" element={<Movie />} />
</BrowserRouter>
2- Add Link
to your image :
<Link to={`movie/${item?.id}`}>
<img
className="w-full h-auto block"
src={`https://image.tmdb.org/t/p/w500/${item?.backdrop_path}`}
alt={item?.title}
/>
</Link>
The idea is to pass an id to a dynamic page depend on this id.<br>
Make sure that you have id
property in your data.
3- Make a new component Movie.js
:
const Movie = () => {
let { id } = useParams();
// Fetch the data depend on the "id" or just filter it
return (
// Your Code here
)
}
Of course don't forget to import the BrowseRouter
Route
Link
Movie
useParams
I wish that was clear for you
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论