Hide CSS background with React onclick

This particular div is responsible for creating a red box. However, my goal is to remove it upon clicking. I attempted to use state to achieve this functionality but unfortunately, it did not work as expected.

import React, { Component } from "react";

class ClickMe extends Component {
  render() {
    return (
      <div>
        <h1>Click to Make Me Disappear</h1>
        <h3>Go ahead, click on the box!</h3>
        <div
          style={{
            height: "400px",
            width: "400px",
            background: "#ef8989",
            margin: "auto",
          }}
        ></div>
      </div>
    );
  }
}
export default ClickMe;

    );
  }
}
export default ClickMe;

Answer №1

Check it out:

import React, { useState } from "react";

const ClickMe =() => {
  const [visible, setVisible] = useState(false)

  return (
    <div onClick={() => setVisible(!visible)}>
      <h1>Vanishing Container</h1>
      <h3>Click the Container if you dare</h3>
      <div
        style={{
          height: "400px",
          width: "400px",
          background: visible ? "none" : "#ef8989",
          margin: "auto",
        }}
      ></div>
    </div>
  );
}

export default ClickMe;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Refreshed the page following a setAttribute function call in Javascript

I have successfully implemented a function to switch between light and dark mode on my webpage using VueJS. However, I am facing an issue with the code: <a @click="toggleTheme" class="switch-mode" href> <div class=&q ...

Confirming Bootstrap - Implement an onConfirm method for element attributes

I am looking to add a JavaScript function to my confirmation button. I have created the elements off-page and fetched them using ajax, so it is crucial that I can set the function in-properties rather than via $('#id').confirmation() So far, I h ...

JavaScript is throwing an error when clicking on functions and managing the z-index property

I am experiencing difficulties with my website code. I have defined all the necessary functions and CSS, but I keep encountering errors such as "clicked is not defined," even though it is clearly present in the script. The goal of the code is to bring the ...

Having trouble with Vue i18n and TypeScript: "The '$t' property is not recognized on the 'VueConstructor' type." Any suggestions on how to resolve this issue?

Within my project, some common functions are stored in separate .ts files. Is there a way to incorporate i18n in these cases? // for i18n import Vue from 'vue' declare module 'vue/types/vue' { interface VueConstructor { $t: an ...

Managing images in JavaScript while conserving memory

Welcome I am embarking on a project to construct a webpage using HTML, CSS, JavaScript (jQuery), and nearly 1000 images. The length of the HTML page is substantial, around 5000px. My vision involves creating a captivating visual experience for users as t ...

You cannot use objects as a React child within the react-stacked-center-carousel component

Using the react-stacked-center-carousel, I have encountered an issue with passing an array of objects to the StackedCarousel component. Here is the link to my reference sandbox: Reference Sandbox I also have a sandbox where I am trying to implement it mys ...

Solving issues with complex GraphQL queries that do not align with the database schema

Short version: How can nested GraphQL queries be resolved when the GraphQL Schema differs from the data stored in a MongoDB database? In our application, each user in our database has an array of foreign keys referencing other documents, such as "pets," w ...

Having trouble with the background-image not displaying and the image not showing up as expected?

I am experiencing an issue with my background image not displaying on my website. I am utilizing HTML5 along with CSS. Below is the code snippet in question: body { width: 1000px; background-image: url(background.jpg); background- ...

What is the best way to retrieve the parameters and query strings from a Next.js Link within the getServerSideProps function?

I am facing an issue with a component that links to a NextJS page where I need to access both the params and the query string using getServerSideProps. My goal is to have a clean URL structure like 'domain.com/username/likes' without any query p ...

Error: WebAssembly.instantiate() encountered a TypeError when trying to import module #0 named "env". The error indicates that the module is not a valid object or

We are currently involved in a project utilizing Next.js and Three.js (react-three-fiber). However, after clearing the cache in the browser, the 3D model disappeared and we encountered some errors. Specifically, there was one warning and one error that kep ...

What's the reason behind the malfunction of this code on Chrome?

After recently installing Mobiscroll, I have been using the date scroller feature with the following code to manage the textbox. <script type="text/javascript"> $(function() { // create a datepicker with default settings $("#begi ...

Using JQuery to loop through elements when clicked

I've been experimenting with different methods to iterate through a series of 4 li elements, each having a class of "item_n" where n is a number from 1 to 4. I want the iteration to increase by 1 with every click. Despite my efforts, my code isn' ...

Issues with aligning text in a flexbox using Bootstrap 4's text-center property

I am currently working on a project to create a webpage with two vertical columns that are clickable and lead to different pages. Here is what I have so far. HTML <!-- Choices --> <div class="container-fluid"> <div class="row"> ...

Is there a way to eliminate the # sign from hash data using jQuery?

Can anyone help me retrieve the hash value from the URL? var hash = window.location.hash; I am looking for a way to remove the "#" sign from the hash. Any suggestions? ...

Display a snippet of each employee's biography along with a "Read More" button that will expand the content using Bootstrap, allowing users to learn more about each team

I have successfully developed a Google App Script that links to a Google Sheet serving as a database. The application iterates through each row, and I showcase each column as part of an employee bio page entry. ex. Picture | Name | Title | Phone | Email ...

Issue with the gulp-babel plugin: Files within the Plugin/Preset should only export functions, not objects

I have started to integrate JavaScript 2015 (ES6) into my Ionic v1 app: package.json { "name": "test", "version": "1.0.0", "dependencies": { "@ionic-native/deeplinks": "^4.18.0", "cordova-android": "7.0.0", "cordova-android-support-gra ...

Typescript Server Problem: Critical Error - Mark-compacts Inefficiently Close to Heap Limit, Allocation Unsuccessful - JavaScript Heap Exhausted

Whenever I run my CRA project, I consistently encounter this error in my console. It seems to be related to the typescript server. Is there a solution for this issue? 99% done plugins webpack-hot-middlewarewebpack built preview 7c330f0bfd3e44c3a97b in 64 ...

Displaying a JSON object in a component's state through appending

I am currently working with a function that takes the state of an object as input and performs various operations on it. After these operations, I need to update the state accordingly and display it in JSON format. Here is the initial state: state= { ...

Styling using CSS will not be effective on anchor tags that do not have the href attribute specified

My coding workflow involves utilizing SASS, and I recently implemented a code snippet to style anchor tags. Here's the code snippet: a { color: $col-accent; &:hover { color: $col-alt; background-color: $col-accent; text-decoration: n ...

Preventing Servlet Redirection in HTML: A Step-by-Step Guide

Is there a way to send data from an HTML web page to a server using a servlet without redirecting? <form method="post" name="customerForum" action="addCustomer"> <button class="btn btn-success" id="ad ...