如何显示此 API 中的单个产品
How can I display a single product from this API
我想展示这个API的产品?
我不想显示所有这些,只显示与 productCode param
关联的那个
import React, {useEffect, useState} from 'react';
import { useParams } from 'react-router-dom';
function ItemDetail() {
const { productCode } = useParams(); // <-- access `productCode` param
const [item, setItem] = useState({});
const token = "ace1a6fd-a483-3de5-9df7-6c9b3acaa93b";
useEffect(() => {
// reference `productCode` in request
fetch(
`https://api.flash-internal.flash-group.com/ecommerceManagement/1.0.0/api/product/${productCode}`,
{
method: "GET",
headers: { Authorization: `Bearer ${token}` },
}
)
.then((res) => res.json())
.then((json) => setItem(json));
}, [productCode]); // <-- add `productCode` as dependency
console.log(productCode);
return (
<div>Item Detail Page</div>
)
}
export default ItemDetail;
在接下来的部分试试这个:
then((json) => {Object.keys(json).map(item => item.id == productCode.id ? setItem(item) : ") });
我想展示这个API的产品?
我不想显示所有这些,只显示与 productCode param
关联的那个import React, {useEffect, useState} from 'react';
import { useParams } from 'react-router-dom';
function ItemDetail() {
const { productCode } = useParams(); // <-- access `productCode` param
const [item, setItem] = useState({});
const token = "ace1a6fd-a483-3de5-9df7-6c9b3acaa93b";
useEffect(() => {
// reference `productCode` in request
fetch(
`https://api.flash-internal.flash-group.com/ecommerceManagement/1.0.0/api/product/${productCode}`,
{
method: "GET",
headers: { Authorization: `Bearer ${token}` },
}
)
.then((res) => res.json())
.then((json) => setItem(json));
}, [productCode]); // <-- add `productCode` as dependency
console.log(productCode);
return (
<div>Item Detail Page</div>
)
}
export default ItemDetail;
在接下来的部分试试这个:
then((json) => {Object.keys(json).map(item => item.id == productCode.id ? setItem(item) : ") });