📅  最后修改于: 2022-03-11 15:02:31.111000             🧑  作者: Mango
import React, { useState } from "react";
const productData = [
{ id: 1, label: "product1", description: "description1" },
{ id: 2, label: "product2", description: "description2" },
{ id: 3, label: "product3", description: "description3" },
{ id: 4, label: "product4", description: "description4" },
{ id: 5, label: "product5", description: "description5" },
{ id: 6, label: "product6", description: "description6" },
{ id: 7, label: "product7", description: "description7" },
];
const Products = () => {
const [showModal, setShowModal] = useState(false);
const [activeObject, setActiveObject] = useState(null);
function getClass(index) {
return index === activeObject?.id ? "active" : "inactive";
}
// here className can not be "inactive" since Modal always shows activeObject
const Modal = ({ object: { label, description } }) => (
This is modal
{label}
{description}
);
return (
<>
{productData.map(({ id, label, description }) => (
- {
setActiveObject({ id, label, description });
setShowModal(true);
}}
className={getClass(id)}
>
{label}
))}
{showModal ? : null}
>
);
};
export default Products;