Shando
Shando

Reputation: 42

Cannot change background color with OnClick

I am building a small app that is a mock-up of https://emergencycompliment.com using ReactJS functional components (actually just the 1 so far). I have implemented a function that is called on the OnClick of a button and is supposed to randomize a compliment to display and change the colour of the background.

This is what I have built (React and CSS)

import React, {useState}from "react"
import "./App.css"

function App() {
  const [compliment, setCompliment] = useState("Hello")
  const [color, setColor] = useState("lightblue")

  const compliments = [
    "You're pretty",
    "You're smart",
    "You're test1",
    "You're test2",
    "You're nice",
    "You're test3"
  ]

  function giveCompliment() {
    let item = compliments[Math.floor(Math.random() * compliments.length)];
    setCompliment(item)
    setColor("green")
  }

  return (
    <div className="App">
      <header className="App-header" style={{backgroundColor: {color}}}>
        <div className="centered">
          <p>
            {compliment}
          </p>
          <button className="button" onClick={giveCompliment}>Thanks!</button> <button className="button">MEH!</button>
        </div>
      </header>
    </div>
  );
}

export default App;
.App {
  text-align: center;
}

.centered {
  position: fixed;
  top: 50%;
  left: 50%;
}

.button {
  background-color: Transparent;
  background-repeat:no-repeat;
  border: 5px solid #f5f5f5;
  font-size: 20px;
  color: whitesmoke;
  width: 100%;
}

.App-logo {
  height: 40vmin;
  pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
  .App-logo {
    animation: App-logo-spin infinite 20s linear;
  }
}

.App-header {
  background-color: #282c34;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px + 2vmin);
  color: white;
}

.App-link {
  color: #61dafb;
}

@keyframes App-logo-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

I have also tried to use background-color={color} without success. For a reason I have yet to find the app sticks to the attribute defined in the CSS (either App or App-header) depending on my tests. I tried to set the color in both App and App-header with no difference. If I remove that background color attribute from the CSS, the page goes white and still no color is changing. I even tried to hardcode a color value but still doesn't work. I have also tried using hex values for color and it was still not working.

Is there something I am missing here?

Upvotes: 0

Views: 559

Answers (2)

dev_integral
dev_integral

Reputation: 112

Ran your code just now. color declared in the useState is a string not an object. on the header tag, remove the curly braces around color like so:

    < header className="App-header" style={{backgroundColor: color}} >

it works!

Upvotes: 2

RoshaanAli
RoshaanAli

Reputation: 156

Use This onClick={()=> giveCompliment()}

Upvotes: -1

Related Questions