Z
Z
zjoin2015-12-17 13:31:59
JavaScript
zjoin, 2015-12-17 13:31:59

How to delay onMouseEnter in React?

People, in general, there is such a state of affairs:

toggleHover() {
    this.setState({ hover: !this.state.hover });
  }

Making an event:
onMouseEnter={this::this.toggleHover} onMouseLeave={this::this.toggleHover}>

How to make make a delay or smooth element?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Gushchin, 2015-12-17
@zjoin

I only recommend making this function via debounce (or equivalents):

constructor(props) {
    super(props);
    const finalMouseDelay = props.mouseDelay || 300;
    
    this.handleMouseLeave = this.handleMouseLeave.bind(this);
    this.handleMouseEnter = this.handleMouseEnter.bind(this);
    this.toggleHover = _.debounce(this.toggleHover.bind(this), finalMouseDelay);
    
    this.state = { hover: false };
  }

  handleMouseLeave() {
    this.toggleHover(false);
  }
  
  handleMouseEnter() {
    this.toggleHover(true);
  }

  toggleHover(to) {
    this.setState({ hover: to });
  }

A working example is here - jsfiddle.net/yuvuyrhv/24
Otherwise you will have wild lags if the user moused over/mouse down/over again... at an interval faster than the one you set

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question