Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make CSS Background disappear onclick with React

The style within the div creates a red box. Upon clicking I want to remove it. Have tried implementing state but it wasn’t working.

import React, { Component } from "react";

class ClickMe extends Component {
  render() {
    return (
      <div>
        <h1>Disapearing Box</h1>
        <h3>Click the Box, I dare you</h3>
        <div
          style={{
            height: "400px",
            width: "400px",
            background: "#ef8989",
            margin: "auto",
          }}
        ></div>
      </div>
    );
  }
}
export default ClickMe;
    );
  }
}
export default ClickMe;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

try it:

import React, { useState } from "react";

const ClickMe =() => {
  const [active, setActive] = useState(false)

  return (
    <div onClick={() => setActive(!active)}>
      <h1>Disapearing Box</h1>
      <h3>Click the Box, I dare you</h3>
      <div
        style={{
          height: "400px",
          width: "400px",
          background: active ? "none" : "#ef8989",
          margin: "auto",
        }}
      ></div>
    </div>
  );
}

export default ClickMe;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading