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

192
Views
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
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!