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

186
Vistas
How to make button color change if button selected and unselected

I need help to add a feature like if the user clicked on the button it changes the color to danger of a button so it would feel like it is selected and if the user clicks the same button it should come back to the original color like primary.

I want to make it a little dynamic as well so it should know which button is being selected in the mapped objects.

I have the following code for now.

       <Row xs={3} md={4} className="g-3 py-2 px-2">
          {data.map((postData) => {
            return (
              <Button
                className="mx-2"
                style={{
                  width: "10rem",
                  height: "13rem",
                  lineHeight: "14px",
                  fontSize: "12px"
                }}
                key={postData.id}
              >
                <Card.Img variant="top" src={postData.image} />
                <Card.Body>
                  <Card.Title className={style.tile}>
                    {postData.title}
                  </Card.Title>
                </Card.Body>
              </Button>
            );
          })}
        </Row>

It's also running in the CodeSandBox

If you can give me a little idea of how to approach this?

I tried using useState for that purpose

onClick={() => setCls((cls) => (cls === "red" ? "green" : "red"))

But that changes the color of all the buttons at one click, you can see this in codesandbox.

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

0

try this Code :

import React, { useState } from "react";

//react bootstrap components
import { Container, Row, Card, Button } from "react-bootstrap";

//scss
import style from "./styles.module.scss";

//data for post
import data from "./data.json";

const App = () => {
  const [selectedBtnList,  setSelectedBtnList] = useState([])

const selectBtnAddHandler = (btnId) => {
 const isInList = selectedBtnList.some(item => item === btnId)
 if(isInList){
  setSelectedBtnList(selectedBtnList.filter(item => item !== btnId))
 }else {
  setSelectedBtnList([...selectedBtnList,btnId])
 }
}
  return (
    <>
      <Container fluid={true}>
        <Row xs={3} md={4} className="g-3 py-2 px-2">
          {data.map((postData) => {
            return (
              <Button
                className="mx-2"
                style={{
                  width: "10rem",
                  height: "13rem",
                  lineHeight: "14px",
                  fontSize: "12px"
                }}
                key={postData.id}
                variant={selectedBtnList.includes(postData.id) ? "danger" : "primary"}
                onClick={()=> selectBtnAddHandler(postData.id)}
              >
                <Card.Img variant="top" src={postData.image} />
                <Card.Body>
                  <Card.Title className={style.tile}>
                    {postData.title}
                  </Card.Title>
                </Card.Body>
              </Button>
            );
          })}
        </Row>
      </Container>
    </>
  );
};

export default App;
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