Show element or text on hover in React
Show element or text on hover in React:
-
onMouseOver
Set the and attributes on the elementonMouseOut
. - Track whether the user is hovering over an element in a state variable.
- 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;
Code example showing how to display one element when you hover over another element.
We set onMouseOver
the property on the div element, so every time the user hovers over the element, handleMouseOver
the 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
handleMouseOver
function, we simplyisHovering
set the state variable to true .
const handleMouseOver = () => {
setIsHovering(true);
};
Instead, in our handleMouseOut
function, 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:
- The handleMouseOver function is called.
- The isHovering state variable is set to true.
- Another div element is rendered.
Conversely, when the user moves the cursor outside the div element:
- The handleMouseOut function is called.
- The isHovering state variable is set to false.
- 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;
div
The code example shows a component when we hover over the element.
We extract a div
and a h2
into the Heading component.
The Heading component will be displayed every time the user hovers over the div with the onMouseOver
and onMouseOut
properties 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.
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.
React tutorial: Types of Props for child components
Publish Date:2025/03/16 Views:172 Category:React
-
Usually, the child components of a React component are a group, that is, the child components are an array. Introduction to Type of the Children Props.
How to solve the error Uncaught TypeError: Cannot read properties of undefined in
Publish Date:2025/03/16 Views:153 Category:React
-
In the process of React development, we often encounter some errors. Here we look at an error reported in App.js. The error is as follows: App.js:69 Uncaught TypeError: Cannot read properties of undefined (reading 'setState') at onInput
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.
Error in React: Attempted import error 'X' is not exported from Solution
Publish Date:2025/03/16 Views:78 Category:React
-
In React, the error “Attempted import error 'X' is not exported from” in React.js occurs when we try to import a named import that does not exist in the specified file. To fix the error, make sure the module has named exports and you have not obfu