Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

show modal by using id in list react

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 } }) => (
    <div id="productModal" className="active">
      This is modal
      <h2>{label}</h2>
      <span className="description">{description}</span>
      <button onClick={() => setShowModal(false)}>Close me</button>
    </div>
  );

  return (
    <>
      <ul className="list-menu">
        {productData.map(({ id, label, description }) => (
          <li
            key={id}
            onClick={() => {
              setActiveObject({ id, label, description });
              setShowModal(true);
            }}
            className={getClass(id)}
          >
            <h2>{label}</h2>
          </li>
        ))}
      </ul>
      {showModal ? <Modal object={activeObject} /> : null}
    </>
  );
};

export default Products;
Comment

PREVIOUS NEXT
Code Example
Javascript :: updating json object in mysql database 
Javascript :: how to get last item in array in javascript 
Javascript :: pdf.js extract text 
Javascript :: clone aJavaScript object 
Javascript :: how set type of string value to number in js 
Javascript :: mongodb find and update one field 
Javascript :: promise all 
Javascript :: vs code file nesting 
Javascript :: date compare in js 
Javascript :: splice js 
Javascript :: javascript hoisting 
Javascript :: infinite scroll for chat react js 
Javascript :: Javascript using forEach loop to loop through an array 
Javascript :: js naming conventions 
Javascript :: image upload using jquery ajax 
Javascript :: how to append in javascript 
Javascript :: usestate in react js 
Javascript :: difference between two time 
Javascript :: jest express testing 
Javascript :: vue directive 
Javascript :: how to add the click event into two element in jquery 
Javascript :: const name value = event.target 
Javascript :: how to input from user in javascript 
Javascript :: copia array javascript 
Javascript :: react file preview 
Javascript :: how custom angular material component date format 
Javascript :: for in loop javascript 
Javascript :: js element on mouse over 
Javascript :: how to slice array in angular 6 
Javascript :: get string length js 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =