![]() However, this got me confused at the beginning because it broke some of the mental models I originally had when it comes to defining animations. I quickly learned that it's possible to propagate changes of variants from a parent motion component to any child motion component. One of the first advanced patterns I got to encounter when I tried to add some micro-interactions with Framer Motion on my projects is propagation. Your feedback is super important and will help me do better for my future blog posts □! Propagation Let me know what you think about these examples, and whether they were helpful. In most situations, you would want to use the onMouseLeave event handler in React.Like the original blog post, this article contains a series of interactive widgets and playgrounds with preset examples to allow you to apply the Framer Motion concepts we're about to see without the need to set up anything! It’s not common that you’d want this, and therefore I advise against using the onMouseOut event. If we use the onMouseOut event handler, anytime the mouse cursor leaves a decendant of that element, the event would trigger. When an event bubbles, it moves, or propagates, up the DOM hierachy. The only difference between onMouseOut vs onMouseLeave is that the onMouseLeave event does not bubble. I can almost hear you screaming your next question, “What is the difference between onMouseLeave and onMouseOut?” There are two additional hoverable event handlers in React, one of which is the onMouseOut event handler. ![]() ![]() You should see the button’s background color change to red. Save this component, jump over to your running React app in your browser and hover over the button. The changeBackground function receives the event object (which is passed automatically through any event handler to a function), and changes the style.background value to ‘red’. We declare a function called changeBackground above the view part of the React Component. After declaring that this element has an onMouseEnter event handler, we can choose what function we want to trigger when the cursor hovers over the element. We do this by adding onMouseOver to the button element. What we’re doing in the code above is, much like the onClick event handler in React, attaching an event handler to the element. Let’s take a look at how we can show or hide an element when hovering over another element in React: If you’d like to learn more, check out my tutorial on Simplifying React State Using the useState Hook. We’ll need to use state for this, therefore we’ll learn about the useState Hook in React. Showing or hiding something is a fairly common UI pattern when hovering over another UI element. Therefore, React has provided the following event handlers for detecting the hover state for an element:Įxample: Show and Hide Something When Hovering Over Another ElementĪs always, let’s begin with a nice simple example. When hovering an element, we want to detect the following states for an HTML element: Why wouldn’t there be an onHover event in React? Well, it wouldn’t give us much control or flexibility to handle the hover state for an element. It’s fairly common to assume that there is an onHover event handler in React, especially when you consider the naming conventions of the other event handlers, such as onClick, onSubmit, and onDrag. When it comes to React event handlers and onHover: The onHover event handler does not exist in React. ![]() You’d think that the onHover event handler exists in React. Instead, we have the onMouseDown, onMouseLeave, and onMouseEnter events to perform onHover actions in React. There is no onHover event handler in React. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |