What is the best way to customize the hover effect in inline CSS on a React website?

After changing my CSS to inline CSS, I encountered an issue when trying to change the button:hover style using inline CSS. Unfortunately, it's not working as expected. Is there a solution to this problem?

<button 
  style={{
    borderRadius: "10px",
    backgroundColor: "#004577",
    color: "#ffffff",
    border: "0px",
    width: "auto",
  }}
  className=" d-flex col-4 justify-content-center  p-2" 
  type="submit">
    Update profile
</button>

I have attempted to fix this issue without success.

Answer №1

Here is the solution I came up with.

import React, { useState } from "react";

function ToggleButton() {
  const [isHovered, setIsHovered] = useState(false);

  const handleMouseEnter = () => {
    setIsHovered(true);
  };

  const handleMouseLeave = () => {
    setIsHovered(false);
  };

  return (
    <button
      style={{
        borderRadius: "10px",
        backgroundColor: isHovered ? "#002744" : "#004577",
        color: "#ffffff",
        border: "0px",
        width: "auto"
      }}
      className="d-flex col-4 justify-content-center p-2"
      type="submit"
      onMouseEnter={handleMouseEnter}
      onMouseLeave={handleMouseLeave}
    >
      Update Profile
    </button>
  );
}

export default ToggleButton;

Answer №2

Unfortunately, it is impossible to do so. However, you can observe this behavior by using the following two props when hovering over or leaving the element:

<button onMouseEnter={() => {console.log('hovered');}} onMouseLeave={() => {console.log('left');}} />

For more information on a similar topic, check out this post here

Answer №3

When it comes to changing styles on hover, there are some adjustments made in Diego Ammann's Answer.

import React, { useState } from "react";

function App() {
  const [isHovered, setIsHovered] = useState(false);

  const handleMouseEnter = () => {
    setIsHovered(true);
  };

  const handleMouseLeave = () => {
    setIsHovered(false);
  };
const non_hover_style= {
        borderRadius: "10px",
        backgroundColor:  "cyan",
        color: "#ffffff",
        border: "0px",
        width: "auto"
      }

const hover_style= {
        borderRadius: "10px",
        backgroundColor: "red" ,
        color: "#ffffff",
        border: "0px",
        width: "auto"
      }

  return (
    <button
      style={isHovered ? hover_style : non_hover_style }
      className="d-flex col-4 justify-content-center p-2"
      type="submit"
      onMouseEnter={handleMouseEnter}
      onMouseLeave={handleMouseLeave}
    >
      Update profile
    </button>
  );
}

export default App;

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

Discovered a single low-impact vulnerability while utilizing React and attempting to serve two files simultaneously

I encountered an error with Node.js that is causing issues when using ReactJS. It seems like the App.js and index.js files are being rendered separately, leading to functions executing twice. I suspect this error may be contributing to this issue. Can an ...

Svelte's glitchy flip animation

My Svelte application includes a list with items that feature a built-in flip animation when they are moved. Each item also contains an absolutely positioned menu with a z-index of 10. However, when the flip animation is triggered, the menu falls behind th ...

When hovering over an anchor, apply a border or box-shadow effect to the image

I'm trying to make an image display a border or box-shadow when hovering over a link, along with an animated circle around the round image. I've attempted to achieve this with the following CSS code, but it doesn't seem to be working. https ...

What are some techniques for managing scrolling within a particular element?

I am currently working with Vue.js and utilizing Element UI components. I want to incorporate a scroll management function to achieve infinite scrolling. To better understand, please refer to the screenshot in the Example section: Despite trying differen ...

Mouse hover is not triggering the animation for the background color

Currently working on a website project and looking to add a "Contact Us" page. Trying to animate the background color of .emailSelectorContainer but it's not quite cooperating. Here’s what I have so far: $(".emailSelectionContainer").hover(function ...

Using Python and Selenium to locate and click the like button on YouTube

Seeking guidance on how to locate and click the YouTube Like button using Python and Selenium, as it lacks a distinct identifier. Any tips or insights? Grateful for any assistance. ...

Does anyone know of a CSS/JavaScript framework that can be used to replicate the look and functionality of the iOS home

I'm wondering if there is a CSS/JavaScript framework that replicates the layout of an iOS home screen. I want to create a kiosk website with a grid layout similar to Android/iOS where apps can be displayed as icons. ...

Struggling with configuring AngularJS?

I've been working on a simple setup, but I can't figure out why AngularJS isn't displaying the content within the curly brackets. It's got me completely baffled. If you'd like to check it out, here's the link to the plunker: ...

Replicating a website to use at a later time

I am brand new to the world of Javascript, currently diving into JavaScript and jQuery for an issue I'm facing. Here's what I'm trying to accomplish: I have a selection of words ("foo", "bar") Retrieve the current webpage's content ( ...

Storing Node_modules in a separate directory

All my node modules are located in the path C:/Users/user/AppData/Roaming/npm/node_modules. I am attempting to include these node modules with babel and babel-presets for my webpack scripts. Here is my webpack.config.js module.exports = { context: &a ...

Having trouble with Bootstrap dropdowns not opening when clicking with jQuery?

I am in the process of developing a table with multiple rows, each containing an "Options" button to display a dropdown context menu. To streamline the code, I am utilizing a single div to serve as a common markup for the context menu. The technologies I ...

Accessing pseudo elements when their sibling is hovered can be achieved by using CSS selectors and combinators

I am struggling with the following html code: <div class="content"> <h2><a href="#">Hero</a></h2> <h2>Hero</h2> <div class ="tricky"></div> </div> The "co ...

The never-ending dilemma of React: should we alter the state array or leave it in its original

My question is regarding state mutations. I came across a suggestion on Cannot see updates from child component through parent's callback (case array and push) that mutating state directly is not recommended. As demonstrated in https://codepen.io/jad ...

What are some ways to dynamically alter the CSS properties of HTML elements at run time using JavaScript or PHP?

As I worked on creating a PHP website with various php pages, I decided to organize the header and footer by putting them in separate php files and including them using the include function. An issue arose when inserting links to the main menu in both the ...

IE6 disrupts stored layouts

I've encountered a strange issue with my design in IE6. It loads perfectly at first, but after reloading it a couple of times, everything suddenly collapses. The strange part is that when I upload an updated version, it fixes the issue, only to break ...

Does Webkit reserve the class name ".ad-space" for a specific purpose?

Can you believe this? I'm working with 960.gs and for some reason my ads class labeled ".ad-space" is vanishing in Webkit browsers. It functions properly in Firefox, but not in Chrome or Safari. Could it be a bug in a Webkit addon, like Adblock Plus? ...

React component using Highcharts is displaying categories as numeric values instead of text

Incorporating the highcharts-react package (https://github.com/highcharts/highcharts-react) into my Laravel project has presented a unique challenge. I am attempting to fetch data from my reducer and transform it into a string to populate the categories in ...

Setting the current date in a Material-UI TextField can be done by utilizing the appropriate props and functions

When setting type="date" on a TextField, it is possible to initialize it with a string. https://codesandbox.io/s/9y6yz462op const values = { someDate: "2017-05-24" }; function App() { return ( <div className="App"> <TextField ...

When looking for a selection, the drop-down list unexpectedly shifts upwards

When using the ngx-intl-tel-input library in my Angular application, I noticed that when searching for a country in the dropdown list, it moves to the top. I am looking to fix the position of the dropdown menu while searching. Check out the demo: Demo Lin ...

Concealment vs Deletion of DOM components

Conceal or Erase When dealing with changing DOM elements, is it better to hide them or delete them? Consider that the environment may change frequently and that elements can have various event callbacks. Hide: What is the best approach when hiding eleme ...