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

177
Views
Change individual numbers color in react

I have this function in React to change the buttons' color. But it changes all buttons and not just an individual one. How can I do that?

  function changeButtonColor() {
    setNumbersColor(numbersColor === 'green' ? 'red' : 'green');
  }

  const [numbersColor, setNumbersColor] = useState('green');
  return (
        <NumbersContainer>
          {numbersList.map((num) => (
            <Numbers
              key={num}
              style={{ backgroundColor: numbersColor }}
              onClick={changeButtonColor}
            >
              {formatNumber(num)}
            </Numbers>
          ))}
        </NumbersContainer>
)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can refactor the numbers component by it self, and make it responsible for it's own colors

import React from "react"

const Button = (props) => {
  const [numbersColor, setNumbersColor] = useState("green")
  
  const changeButtonColor = ()=>{
    setNumbersColor(numbersColor === 'green' ? 'red' : 'green')
  }

  return(
    <button
       style={{ backgroundColor: numbersColor }}
       onClick={changeButtonColor}
     >
     {props.children}
     </button>

  )
}

const NumbersList = () => {
    return (
        <NumbersContainer>
          {numbersList.map((num) => (
            <Button key={num}>
              {formatNumber(num)}
            </Button>
          ))}
        </NumbersContainer>
    )
}
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!