Show component when freezing in real life

I have created several sections with the title of specific content.

I want to show a short sneak preview hovering over another section.

Does anyone know how to create a hoverActionHandler with conditional rendering of a react component?

+4
source share
1 answer

You can use onMouseEnterand onMouseLeaveto change state and visualize a component conditionally based on the state value.

See in action: https://codesandbox.io/s/XopkqJ5oV

import React, { Component } from 'react';

class HoverExample extends Component {
  constructor(props) {
    super(props);
    this.handleMouseHover = this.handleMouseHover.bind(this);
    this.state = {
      isHovering: false,
    };
  }

  handleMouseHover() {
    this.setState(this.toggleHoverState);
  }

  toggleHoverState(state) {
    return {
      isHovering: !state.isHovering,
    };
  }

  render() {
    return (
      <div>
        <div
          onMouseEnter={this.handleMouseHover}
          onMouseLeave={this.handleMouseHover}
        >
          Hover Me
        </div>
        {
          this.state.isHovering &&
          <div>
            Hovering right meow! 🐱
          </div>
        }
      </div>
    );
  }
}
+12
source

Source: https://habr.com/ru/post/1679339/


All Articles