Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
Div appear and disappear onclick in React JS

I want to show the content on the cards (marked in red and named product.description in the code) on click but I don't know any way of doing it.

The content on the card is fetched from JSON-server.

Here is some code

The first js is used to fetch the data from the server and display that on the page and the second one is the method of showing the data on the page. In general, I want the p tag to appear and disappear when the user clicks on the main div named Product-Preview

import { useEffect, useState } from "react";
import ProductList from "./ProductList";

const Products = () => {
    const [products, setProducts] = useState (null);

    useEffect (() => {
        fetch('http://localhost:8000/products')
            .then(res => {
                return res.json();
            })
            .then(data => {
                setProducts(data);
            })
    }, []);
    
    return ( 
        <div className="ProductList">
            {products && <ProductList products={products}/>}
        </div>
    );
}

export default Products;
const ProductList = (props) => {
    const products = props.products;


    return ( 
        <div className="ProductList" >
            {products.map((product) => (
                <div className="Product-Preview" key={product.id}>
                    <div className="backdrop" style={{backgroundImage: `url(${product.image})`}}></div>
                    <h2>{ product.title }</h2>
                    <p>{ product.description }</p>
                    <div>{ product.price }</div><br />
                </div>
                
            ))}
        </div>
    );
}

export default ProductList;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should create a component and use a state in this component to do it.

Example component has a name is Card

const Card= ({ product }) => {
  const [showDescription, setShowDescription] = useState(false);

  return (
    <div
      className="Product-Preview"
      onClick={() => setShowDescription(!showDescription)}
    >
      <div className="backdrop" style={{ backgroundImage: `url(${product.image})` }}></div>
      <h2>{product.title}</h2>
      {showDescription && <p>{product.description}</p>}
      <div>{product.price}</div>
      <br />
    </div>
  );
};

And use this component inside map

{
  products.map((product) => <Card product={product} key={product.id} />);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!