Delete the backgroundImage when the component mounts

My website has a <Login/> page and a <Dashboard/>.

The login page is styled with a background color of #222, while the Dashboard page has a background of whitesmoke

To achieve this, I have set the body CSS like this:

body {
    background-color: #222222;
}

And in the Dashboard.js:

componentWillMount() {
  document.body.style.backgroundColor = "whitesmoke";
}
componentWillUnmount() {
    document.body.style.backgroundColor = null;
}

Everything was working fine until I decided to add an image as the background for the Login page, here's how I did it:

body {
    background-color: #222222;
    background: url('../../public/img/bg.png');
    background-repeat: repeat;
}

However, now my Dashboard also shows the same background image. Even when I tried to remove the image in the Dashboard component:

componentWillMount() {
  document.body.style.backgroundImage = null;
  document.body.style.backgroundColor = "whitesmoke";
}
componentWillUnmount() {
  document.body.style.backgroundColor = null;
}

I am facing this issue. How can I resolve it?

Answer №1

How about utilizing classes in this situation?


initializeComponent() {
  $('body').addClass('has-background');
}
cleanupComponent() {
  $('body').removeClass('has-background');
}

In addition, consider abstracting the addClass / removeClass functions and implementing emits.

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

Is there a clash between ASP.NET form reloading triggered by JavaScript and populating a JQuery autocomplete textbox?

Within my ASP.NET form, I have various form elements including a TextBox, a HiddenField, and a DropDownList. These elements are defined as follows: <asp:TextBox ID="_txtData" runat="server" ClientIDMode="Static" /> <a ...

Remove the bootstrap styling from a section of the webpage

I am in the process of setting up a preview box for an HTML editor on one of my pages. I created a basic <div id="preview"></div> style container where I occasionally input my HTML source, which is working adequately. The issue arises when Boo ...

Looking to loop over an array to extract certain key-value pairs and append them to an empty object

So here's the challenge: create a function that takes an array of strings and outputs an object. The keys in the object should represent the number of characters in each string, while the values should indicate how many times a string with that amount ...

Tips for positioning text in the center of an image

I'm struggling to center a text block both horizontally and vertically above an image. I have searched through various resources, but none of the solutions seem to work for me. Below is the code snippet in question: <body> <div id="t ...

What is the reason for receiving an HTML code as the response from an XMLHttpRequest

When I send an http request using xmlhttp, the response I receive is in HTML form for some unknown reason. function HttpRequest(method, url, username, password) { const xhr = new XMLHttpRequest(); let received_data; xhr.onreadystatechange = () ...

How to place a div within another div without using absolute positioning

I've been searching for a solution to this issue, but nothing seems to be working. I want the inner div to be positioned at the bottom of the outer div, with a 5px margin separating them. However, using absolute positioning disrupts the width and cent ...

Enhancing asynchronous loading with Axios interceptors

When utilizing vue.js along with the axios library to make requests to my API, I aim to configure it globally and display a loading message when the request takes too long. I discovered that by using axios interceptors, I can set up axios configuration on ...

Version 2 of the Microsoft Logo Animation

I made some changes to my Microsoft logo animation project. You can view the updated version on CodePen. Overall, I'm pretty happy with how it turned out except for one issue. I'm struggling to determine the right timing to end the animation so ...

Tips for customizing the appearance of React components in MUI 5

I am currently in the process of transitioning my project from Material-UI v4 to MUI v5. I have been referencing the official guide on How to customize, but I am struggling to figure out how to refactor this component according to the new guidelines: ... c ...

Reacting to errors with slice in React JS

Hello, I am facing an issue with the .slice method. The error message reads: ".slice is not a function." I am attempting to display customer commands in a table, but encountering an error. I am fetching commands from my custom API which returns them in ...

Experiencing difficulty establishing a connection between MongoDB and my server.js file

An error has occurred: throw new error_1.MongoParseError(${optionWord} ${Array.from(unsupportedOptions).join(', ')} ${isOrAre} not supported); I encountered this error while working on my code in server.js file. const express = require('exp ...

Using Javascript to automatically submit a form when the enter key is pressed

Can anyone help me with a password form issue? I want the enter key to trigger the submit button but it's not working for me. I understand that the password can be viewed in the source code, this is just for practice purposes. <!DOCTYPE html> & ...

Experience the potential of HTML5 by playing dual audio MKV/AVI videos

After conducting some research, it seems that Chrome has the necessary codecs to play MKV videos. However, I have yet to come across any information on how to select audio tracks in MKV and AVI files using Javascript/HTML5. Does anyone have any insight in ...

How can I connect a React component to a static HTML page?

Currently, I am working on a React + Redux application and have come across an issue. I am trying to create a link to a privacy.html page that is located in the root of the project alongside index.html (which is the React app). The problem I'm facing ...

Choosing the default checkbox option as selected in a ReactJS application

Is there a way to preselect checkboxes in ReactJS based on certain output data? For example, I have the following output: {permission:{group:["can_view"],"topGroup":["can_update"]}} . After sending this data to the database and receiving back the edited ...

move position when clicked-javascript animation

Is there a way to create a button that changes a div's position with a 2-second transition on the first click and then returns it back on the second click? I tried implementing this code, but unfortunately, it doesn't bring the div back. va ...

Problem with React Router: Uncaught Error - Invariant Violation: The element type is not valid, a string is expected for built-in components

I am encountering an issue with react-router and unable to render my app due to this error. Here is a screenshot of the error I have searched extensively for a solution but have not been able to find anything useful. Any help would be greatly appreciated ...

Unable to properly utilize environment variables on Vercel when using Nuxt framework

I'm encountering difficulties accessing my environment variables on Vercel. When I test the site on my localhost, everything works fine; however, once it's deployed to Vercel, the access to environment variables in my components and plugins direc ...

Tips for expanding an md-nav-item in Angular Material

Need help on stretching the md-nav-item element illustration 1 ------------------------------------------------ | ITEM1 | ITEM 2 | ------------------------------------------------ illustration 2 ------------------------------------------------ | ...

Filtered Owl Carousel

Hello everyone. Just wanted to mention that our English may not be perfect. I managed to filter with owl carousel by tweaking some codes I found online. It's working now, but I'd love for it to have an animated effect while filtering, like a fad ...