JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

Show element or text on hover in React

Author:JIYIK Last Updated:2025/03/03 Views:

Show element or text on hover in React:

  1. onMouseOverSet the and attributes on the element onMouseOut.
  2. Track whether the user is hovering over an element in a state variable.
  3. Conditionally render another element based on a state variable.
import {useState} from 'react';

const App = () => {
  const [isHovering, setIsHovering] = useState(false);

  const handleMouseOver = () => {
    setIsHovering(true);
  };

  const handleMouseOut = () => {
    setIsHovering(false);
  };

  return (
    <div>
      <div>
        <div
          onMouseOver={handleMouseOver}
          onMouseOut={handleMouseOut}
        >
          Hover over me
        </div>

        {isHovering && (
          <div>
            <h2>Only visible when hovering div</h2>
            <h2>jiyik.com</h2>
          </div>
        )}
      </div>
    </div>
  );
};

export default App;

Display element or text on hover in React

Code example showing how to display one element when you hover over another element.

We set onMouseOverthe property on the div element, so every time the user hovers over the element, handleMouseOverthe function will be called.

<div
  onMouseOver={handleMouseOver}
  onMouseOut={handleMouseOut}
>
  Hover over me
</div>

The event is fired when the user moves the cursor over the element or one of its child elements mouseover.

In our handleMouseOverfunction, we simply isHoveringset the state variable to true .

const handleMouseOver = () => {
  setIsHovering(true);
};

Instead, in our handleMouseOutfunction, we set the state variable to false .

const handleMouseOut = () => {
  setIsHovering(false);
};

The event is fired when the user's cursor is no longer contained within the element or one of its child elements mouseout.

We use the logical AND &&operator to conditionally render another div element.

The logical AND &&operator returns the value on the left if it is false, otherwise it returns the value on the right.

If the state variable stores a false value, the logical AND &&operator will return false and nothing will be rendered.

Ignore boolean values, null and undefined. They are not rendered at all.

When the user hovers over the div element:

  1. The handleMouseOver function is called.
  2. The isHovering state variable is set to true.
  3. Another div element is rendered.

Conversely, when the user moves the cursor outside the div element:

  1. The handleMouseOut function is called.
  2. The isHovering state variable is set to false.
  3. The other div element is no longer displayed.

Show component on hover in React

The same method can be used to show a component when you hover over another element.

import {useState} from 'react';

function Heading() {
  return (
    <div>
      <h2>jiyik.com</h2>
    </div>
  );
}

const App = () => {
  const [isHovering, setIsHovering] = useState(false);

  const handleMouseOver = () => {
    setIsHovering(true);
  };

  const handleMouseOut = () => {
    setIsHovering(false);
  };

  return (
    <div>
      <div>
        <div
          onMouseOver={handleMouseOver}
          onMouseOut={handleMouseOut}
        >
          Hover over me
        </div>

        {isHovering && <Heading />}
      </div>
    </div>
  );
};

export default App;

Show component on hover in React

divThe code example shows a component when we hover over the element.

We extract a divand a h2into the Heading component.

The Heading component will be displayed every time the user hovers over the div with the onMouseOverand onMouseOutproperties set .

When the user moves the mouse out of the div, the Heading component unmounts and is no longer rendered.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to avoid cross-origin (CORS) issues in React/Next.js

Publish Date:2025/03/17 Views:170 Category:NETWORK

In this article, we will introduce how to avoid cross-origin (CORS) issues in React/Next.js. Cross-origin resource sharing (CORS) is a protocol that defines how web requests should be handled when crossing different URLs.

React Tutorial - Transferring Props

Publish Date:2025/03/16 Views:188 Category:React

React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.

React Tutorial: Props Anti-Pattern

Publish Date:2025/03/16 Views:187 Category:React

React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.

React Tutorial - Props Validation

Publish Date:2025/03/16 Views:102 Category:React

Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:60 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Solution to the error "does not contain a default export" in React

Publish Date:2025/03/16 Views:191 Category:React

When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial