1

I'm trying to display some information only on hover to special div. I'm using material ui and their withStyles component. Is there a way to do that?

4
  • 1
    Would a Tooltip cover your need? material-ui.com/components/tooltips Commented Apr 16, 2020 at 16:08
  • 1
    Related answer: stackoverflow.com/questions/57716926/… Commented Apr 16, 2020 at 16:16
  • @DavisJones Tooltip from material-ui is good solution, but I need a lot of customization in my case. Not sure that I could do it. Commented Apr 16, 2020 at 16:26
  • @RyanCogswell Thanks! It looks like an elegant solution Commented Apr 16, 2020 at 16:30

1 Answer 1

2

Please check this example:

import React, {useState} from "react";

export default function ShowButtonHover() {
    const [style, setStyle] = useState({display: 'none'});

    return (
        <div className="App">
            <h2>Hidden message in the box. Move mouse in the box</h2>
            <div style={{border: '1px solid gray', width: 300, height: 300, padding: 10, margin: 100}}
                 onMouseEnter={e => {
                     setStyle({display: 'block'});
                 }}
                 onMouseLeave={e => {
                     setStyle({display: 'none'})
                 }}
            >
                <div style={style}>This was hidden</div>
            </div>
        </div>
    );
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I checked this example, it works. Although keep such style property in state seems a little bit overhead, it solves the problem.
@Tatiana Thank you so much
But I still not sure that keep styles in component state is really right way :) Maybe I miss something
As this is just an example, so if you have many styles then you write those styles in css class into a css file then you can use setClassName("your-css-class"). then I am sure you will be worried anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.