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?
Asked
Active
Viewed 950 times
0
-
1Would a Tooltip cover your need? https://material-ui.com/components/tooltips/ – Davis Jones Apr 16 '20 at 16:08
-
1Related answer: https://stackoverflow.com/questions/57716926/how-to-use-css-in-js-for-nested-hover-styles-material-ui/57729101#57729101 – Ryan Cogswell Apr 16 '20 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. – Tatiana Apr 16 '20 at 16:26
-
@RyanCogswell Thanks! It looks like an elegant solution – Tatiana Apr 16 '20 at 16:30
1 Answers
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
-
-
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