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

Using onChange event in React

Publish Date:2025/03/03 Views:148 Category:React

onChange 是 React 中最常见的输入事件之一。本文将帮助你了解它的工作原理。

Checkbox onChange in React

Publish Date:2025/03/03 Views:73 Category:React

本教程演示了如何从 React 中 onChange 事件的复选框发送值。

onDoubleClick in React

Publish Date:2025/03/03 Views:128 Category:React

本教程演示了如何在 React 中使用 onDoubleClick。

Scroll to top of page in React.js

Publish Date:2025/03/03 Views:182 Category:React

In React, use the window.scrollTo() method to scroll to the top of the page, for example, window.scrollTo(0, 0) . The scrollTo method on the window object scrolls to a specific set of coordinates in the document. import {useEffect} from react ; export

Applying global CSS styles in React applications

Publish Date:2025/03/03 Views:145 Category:React

要在 React 应用程序中应用全局 CSS 样式,请将 CSS 写入扩展名为 .css 的文件中,并将其导入 index.js 文件中。 全局 CSS 应该在 index.js 中导入,以确保它被加载到你的 React 应用程序的所有

Passing events and parameters to onClick in React

Publish Date:2025/03/03 Views:63 Category:React

Passing events and arguments onClick in React: Pass an inline function to the onClick attribute of the element. The function should get the event object and call handleClick. Pass the event and arguments to handleClick. const App = () = { const handle

How to remove event listeners in React

Publish Date:2025/03/03 Views:194 Category:React

To remove an event listener in React: Add an event listener in the useEffect hook. Return a function from the useEffect hook. When the component unmounts, remove the event listener using the removeEventListener method. import {useRef, useEffect} from

Using conditions to jump out of a map in map() in React

Publish Date:2025/03/03 Views:147 Category:React

Using conditions in map() in React: Call the map() method on an array. Use an if condition to explicitly return if the condition is met. Otherwise return a different value or return null to render nothing. export default function App () { const arr =

Calling multiple onClick functions in React

Publish Date:2025/03/03 Views:105 Category:React

To call multiple onClick functions in React: Set the onClick attribute on the element. Call other functions in the event handler. The event handler can call as many other functions as needed. export default function App () { const s

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial