简体   繁体   English

React,如何使用 React Router Dom 将产品 ID 传递到详细信息页面?

[英]React, how to pass product id to details page with React Router Dom?

I'm creating a page, like Amazon.我正在创建一个页面,例如亚马逊。 In the index page I have a list of products, and when the user clicks the link it goes to the product page that has all the details of the product, but for me to create this page I've to get the ID from the product but I can't get it.在索引页面中,我有一个产品列表,当用户单击链接时,它会转到包含产品所有详细信息的产品页面,但是对于我来说要创建此页面,我必须从产品中获取 ID但我无法得到它。

App.js:应用程序.js:

<main className="main">
 <div className="content">
  <Routes>
   <Route path="/product/:id" element={<ProductScreen />} />
   <Route path="/" exact element={<HomeScreen />} />
  </Routes>
 </div>
</main>

HomeScreen:主屏幕:

import React from 'react';
import data from "../data";
import { Link } from 'react-router-dom';

function HomeScreen(props) {
    return (
    <ul className="products">
    {
      data.products.map(product => 
        <li>
          <div className="product">
          <Link to={'/product/' + product._id}>
                <img
                src={product.image}
                alt="Product"
                className="product-image"
                />
            </Link>
            <div className="product-name">
              <Link to={'/product/' + product._id}>{product.name}</Link>
            </div>
            <div className="product-brand">{product.brand}</div>
            <div className="product-price">{product.price}</div>
            <div className="product-rating">{product.rating} Leafs ({product.numberReviews} Reviews)</div>
          </div>
        </li>
      )
    }
  </ul>
    );
}

export default HomeScreen;

ProductScreen:产品屏幕:

import React from 'react';
import data from '../data';

function ProductScreen(props) {
    console.log(props.match.params.id)
    return <div>Product Screen</div>
}

export default ProductScreen;

I console.log it to see if everything was okay but the error shown is我 console.log 看看是否一切正常,但显示的错误是

TypeError: Cannot read properties of undefined (reading 'params') TypeError:无法读取未定义的属性(读取“参数”)

What am I missing?我错过了什么? I tried several stuff that I saw on the web.我尝试了一些我在 web 上看到的东西。

You need to use useParams hook from React Router Dom.你需要使用 React Router Dom 中的useParams钩子。 Like below:如下所示:

import React from 'react';
import data from '../data';
import {useParams} from "react-router-dom"

function ProductScreen(props) {
 const {id} = useParams();
 console.log(id)
 return <div>Product Screen</div>
}

export default ProductScreen;

If you want to get the id as a prop you must forward it to the ProductScreen component.如果您想将 id 作为道具获取,则必须将其转发给 ProductScreen 组件。

Now if you want to grab it from the url, you could use the useParams hook from react-router-dom.现在,如果您想从 url 中获取它,您可以使用 react-router-dom 中的 useParams 挂钩。 Guide: https://v5.reactrouter.com/web/example/url-params指南: https://v5.reactrouter.com/web/example/url-params

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM