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

189
Vistas
How can I change color of a selected item in a react map with onClick function?

I'm trying to compare the currently clicked one with hasClip in a given react map and empty the color if result is identical with the id and category whereas fill the color if it's not the same.

Can someone please help me in figuring out how I can resolve this?

Below I have an array where I map out the category key type and item id:

const data = [
  {
    keyType: "category",
    filtered: [
      { _id: 1, name: "Stocks", paramValue: "stocks" },
      { _id: 19, name: "Commodities", paramValue: "commodities" },
    ],
  },
  {
    keyType: "topics",
    filtered: [
      { _id: 8, name: "Preferred stocks", paramValue: "preferred-stocks" },
      { _id: 24, name: "Stocks", paramValue: "stocks" },
    ],
  },
];

const hasClip = [
  { _id: 1, keyType: "category", name: "Stocks", paramValue: "stocks" },
  {
    _id: 8,
    keyType: "topics",
    name: "Preferred stocks",
    paramValue: "preferred-stocks",
  },
];

const handleFill = (hasClip, category, id) => {
  checkKeyword(hasClip, category, id);
};

const checkKeyword = (hasClip, category, id) => {
  const result = hasClip.some(
    (item) => item._id === id && item.keyType === category
  );
  return result;
};

const par = () => {
  return (
    <>
      {data.map((category) => (
        <>
          <h1 key={category.keyType}>{category.keyType}</h1>
          {category.filtered.map((item) => (
            <styledComp
              key={item._id}
              onClick={() => {
                handleFill(hasClip, category.keyType, item._id);
              }}
              $clip={checkKeyword(hasClip, category.keyType, item._id)}
            >
              {item.name}
            </styledComp>
          ))}
        </>
      ))}
    </>
  );
};

export default par;

const styledComp = styled.div`
  width: 50px;
  height: 50px;
  background-color: ${({ $clip }) => ($clip ? "red" : "none")};
`;
about 4 years ago · Santiago Gelvez
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