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

157
Visualizações
Change box color randomly when button is clicked

I want to change color of box randomly when button is clicked. But when I click on button background becomes white. I dont know the reason i am color flipper project and got stuck here. I checked console their color values are generating but it is not getting assigned to background-color: property.

import React,{useState} from 'react'
import styled from 'styled-components'

function Box() {
    function randomRGB(){
        // console.log("random");
        return Math.floor(Math.random() * 256);
    }
    function getRandomColor() {
        const color ='rgb(' + randomRGB() + ',' + randomRGB() + ',' + randomRGB() + ')';
        console.log(color);
        return color;
    }
    var [currentColor, setCurrentColor] = useState(getRandomColor());
  return (
    <div>
        <BoxC back={currentColor}></BoxC>
        <Div onClick={setCurrentColor}>Button</Div>
    </div>
  )
}
var BoxC = styled.div`
    width: 200px;
    height: 200px;
    background: ${(props) => props.back};
`
const Div = styled.button`
    width: 70px;
    height: 50px;
    border: 2px solid green;
    cursor: pointer;
`
export default Box
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You have to assign a new color on click by

onClick={()=>setCurrentColor(randomrgb())}>Button</Div>
about 4 years ago · Juan Pablo Isaza Relatório

0

You are not setting the currentColor state to anything. You are just calling the function.

Do it like this instead:

import React,{useState} from 'react'
import styled from 'styled-components'

function Box() {
var [currentColor, setCurrentColor] = useState(getRandomColor());
    function randomRGB(){
        // console.log("random");
        return Math.floor(Math.random() * 256);
    }
    function getRandomColor() {
        const color ='rgb(' + randomRGB() + ',' + randomRGB() + ',' + randomRGB() + ')';
        console.log(color);
        return setCurrentColor(color);
    }
  return (
    <div>
        <BoxC back={currentColor}></BoxC>
        <Div onClick={() => getRandomColor()}>Button</Div>
    </div>
  )
}
var BoxC = styled.div`
    width: 200px;
    height: 200px;
    background: ${(props) => props.back};
`
const Div = styled.button`
    width: 70px;
    height: 50px;
    border: 2px solid green;
    cursor: pointer;
`
export default Box
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