Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Change style to individual item React-Hooks

I want to create a button that when you click it opens a navbar with different colors that when the user presses one of the colors, the navbar is closed and the button it self take the color the user clicked on.

my code works, but I have different outcomes then what I expected

this is the outcome enter image description here

the green dots all changed at once, i want that only the dot on the item i clicked will change color

this is the code

    const colorLevelArr = ["rgb(236, 77, 77)", "rgb(241, 153, 70)", "rgb(255, 253, 151)", "rgb(147, 241, 135)","rgb(132, 220, 255)"];
    const [colorLevel, setColorLevel] = useState([]);
    const [colorLevelOpen, setcolorLevelOpen] = useState(null);
    
    const toggelColorLevel = (item) => {
    setcolorLevelOpen(preOpen => preOpen === item ? null : item)
}
const changeColorLevel = (index, item) => {
    setColorLevel(colorLevelArr[index])
    setcolorLevelOpen(preOpen => preOpen === item ? null : item)
}

and this is the code for the button

<button className="color_level_container"
onClick={() => toggelColorLevel(item)}
style={{
  backgroundColor:`${colorLevel}`,
      }}>
   {colorLevelOpen === item && (
   <div className="color_btn_container">
           {colorLevelArr.map((colorLevel, index)=>{
               return (
                  <div onClick={()=>{
                      toggelColorLevel(item)
                      changeColorLevel(index, item)
                      }
                  }
                   style={{
                       borderRadius:"100%",
                       border:"1px solid black",
                       width:"15px",
                       height:"15px",
                       backgroundColor:`${colorLevel}`,
                   }}>                                                                            
                  </div>
               ) 
           })}
        </div>
        )}
        
    
</button>

if someone can help me understand how to do it right,

and that the only color change will be to the item I pressed

Thank you in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

it looks like you are using one state for the color, and use it for all the items. the easiest way to fix it will be to create a different state for each button, and by so, each one will have its own color. this can be done by creating a component for the button which holds the state for the color, and when state changes, the color of the current component will change accordingly and wont affect the other button components.

I wrote a small example just for you to understand the idea:

const colorsList = ['red', 'yellow', 'blue', 'green'];

const App = () => {
  return (
    <div style={{ margin: '100px' }}>
      <ChangingColorButton />
      <ChangingColorButton />
      <ChangingColorButton />
      <ChangingColorButton />
    </div>
  );
};

const ChangingColorButton = () => {
  const [color, setColor] = useState('black');

  return (
    <button
      onClick={() => setColor(colorsList[Math.floor(Math.random() * 3) + 1])}
      style={{ backgroundColor: color, marginRight: '8px' }}>
      Click to change color
    </button>
  );
};

as you can see, i have a list of color and by clicking each button its background color change to random color form the list.

This example demonstrate how you can control each color for each button separately.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda