如何基于从 API 拉取的图像选择来导航到新页面在 React 中?

huangapple go评论57阅读模式
英文:

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.

 &lt;div className=&quot;w-[160px] sm:w-[200px] md:w-[240px] lg:w-[280px] inline-block cursor-pointer relative p-2&quot;&gt;
      &lt;img
        className=&quot;w-full h-auto block&quot;
        src={`https://image.tmdb.org/t/p/w500/${item?.backdrop_path}`}
        alt={item?.title}
      /&gt;
      &lt;div className=&quot;absolute top-0 left-0 w-full h-full hover:bg-black/80 opacity-0 hover:opacity-100 text-white&quot;&gt;
        &lt;p className=&quot;white-space-normal text-xs md:text-sm font-bold flex justify-center items-center h-full text-center&quot;&gt;
          {item?.title}
        &lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;

答案1

得分: 0

以下是翻译好的部分:

1- 在你的 App.js 中:

&lt;BrowserRouter&gt;
    &lt;Route path=&quot;movie/:id&quot; element={&lt;Movie /&gt;} /&gt;
&lt;/BrowserRouter&gt;

2- 将 Link 添加到你的图像:

&lt;Link to={`movie/${item?.id}`}&gt;
    &lt;img
        className=&quot;w-full h-auto block&quot;
        src={`https://image.tmdb.org/t/p/w500/${item?.backdrop_path}`}
        alt={item?.title}
    /&gt;
&lt;/Link&gt;

这个想法是根据这个 id 传递给动态页面。确保你的数据中有 id 属性。

3- 创建一个新的组件 Movie.js

const Movie = () =&gt; {
    let { id } = useParams();
    // 根据 "id" 检索数据或进行筛选
    return (
        // 在这里添加你的代码
    )
}

当然,不要忘记导入 BrowseRouterRouteLinkMovieuseParams

希望这对你清楚明了。

英文:

You can use dynamics pages to deal with it

1- In your App.js :

&lt;BrowserRouter&gt;
    &lt;Route path=&quot;movie/:id&quot; element={&lt;Movie /&gt;} /&gt;
&lt;/BrowserRouter&gt;

2- Add Link to your image :

&lt;Link to={`movie/${item?.id}`}&gt;
    &lt;img
        className=&quot;w-full h-auto block&quot;
        src={`https://image.tmdb.org/t/p/w500/${item?.backdrop_path}`}
        alt={item?.title}
    /&gt;
&lt;/Link&gt;

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 = () =&gt; {
    let { id } = useParams();
    // Fetch the data depend on the &quot;id&quot; 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

huangapple
  • 本文由 发表于 2023年3月21日 03:05:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定