Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
toggling styles of multiple buttons onclick in react

hi everyone I'm building my first e-commerce store and ran into an issue, my site is selling shoes. enter image description here

but when I click on the button I want to be able to change the color of the selected button I should only be able to select one button as you cant have two sizes per shoe.

here is the component containing the shoe card:

import React from "react";

const ShoeProductCard = ({ productData }) => {

  const sizes = [7, 8, 9, 10];


  return productData.map((product) => {
    
    
    return (
      <div key={product.id} className="productsGrid">
        <div key={product.id} className="container">
          <div
            key={product.id}
            style={{ backgroundColor: product.color }}
            className="card"
          >
            <div key={product.id} className="imgBx">
              <img key={product.id} src={product.img} alt="shoes" />
            </div>
            <div className="contentBx">
              <h2>{product.title}</h2>
              <div className="size">
                <h3>Size :</h3>
                {sizes.map((size) => (
                  <span  key={size} >{size}</span>
                ))}
              </div>

              <a href="#" alt="logo">
                {" "}
                <i className="fa-solid fa-cart-arrow-down"></i>
              </a>
              <br />
              <span className="price">Price: {product.price}</span>
              <br />
              <i
                style={{ color: "white", cursor: "pointer" }}
                className="fa-solid fa-circle-info"
              ></i>
            </div>
          </div>
        </div>
      </div>
    );
  });
};

export default ShoeProductCard;

now what I tried was giving my styles a state of true and false and conditionally reading it with my class in the component inline. but that didn't work it just erased the entire page.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This is how I would do it. Have a state variable storing the selected shoe size, which changes when a new size button is clicked.

import React, { useState } from "react";

const ShoeProductCard = () => {
    const sizes = [7, 8, 9, 10];
    const [selectedSize, setSelectedSize] = useState("");

    // dummy data
    let productData = [{ id: 1, img: "imgSrc", title: "Yeezy 350s", price: 780 }];

    return productData.map((product) => {
        return (
            <div key={product.id} className="productsGrid">
                <div key={product.id} className="container">
                    <div
                        key={product.id}
                        style={{ backgroundColor: product.color }}
                        className="card"
                    >
                        <div key={product.id} className="imgBx">
                            <img key={product.id} src={product.img} alt="shoes" />
                        </div>
                        <div className="contentBx">
                            <h2>{product.title}</h2>
                            <div className="size">
                                <h3>Size :</h3>
                                {sizes.map((size) => (
                                    <button
                                        onClick={() => setSelectedSize(size)}
                                        style={{
                                            backgroundColor: selectedSize === size ? "grey" : "white",
                                        }}
                                        key={size}
                                    >
                                        {size}
                                    </button>
                                ))}
                            </div>

                            <a href="#" alt="logo">
                                {" "}
                                <i className="fa-solid fa-cart-arrow-down"></i>
                            </a>
                            <br />
                            <span className="price">Price: {product.price}</span>
                            <br />
                            <i
                                style={{ color: "white", cursor: "pointer" }}
                                className="fa-solid fa-circle-info"
                            ></i>
                        </div>
                    </div>
                </div>
            </div>
        );
    });
};

export default ShoeProductCard;

Basically, I've changed your spans to buttons which are mapped from the array of sizes.

backgroundColor: selectedSize === size ? "grey" : "white",

This here just changes the color of the buttons, if the size has been selected it's grey, if not it's white. Just change accordingly. You may have to set your buttons to display: block to style them how you want.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda