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

156
Vistas
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 Respuestas
Responde la pregunta

0

You have to assign a new color on click by

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

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 Denunciar
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