0

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?

Tatiana
  • 576
  • 1
  • 6
  • 23

1 Answers1

1

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>
    );
}
Khabir
  • 5,370
  • 1
  • 21
  • 33
  • Thanks! I checked this example, it works. Although keep such style property in state seems a little bit overhead, it solves the problem. – Tatiana Apr 16 '20 at 16:23
  • @Tatiana Thank you so much – Khabir Apr 16 '20 at 16:30
  • But I still not sure that keep styles in component state is really right way :) Maybe I miss something – Tatiana Apr 16 '20 at 16:33
  • 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. – Khabir Apr 16 '20 at 16:43