Search code examples
javascriptreactjsmaterial-uicss-in-js

How to display div element on hover another element using css-in-js?


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?


Solution

  • 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>
        );
    }