22

I'm using the animate.css library with React and trying to set up a element (button) to pulse when hovered over. Tried to look through the docs and here but can't find a way to achieve this simple task. If anyone has achieved this or found a reference would greatly be appreciated.

class App extends Component {

   constructor(props) {
    super(props);

    this.handleHover = this.handleHover.bind(this);
  }

  handleHover(){
    this.setState({
        isHovered: !this.state.isHovered
    });
  }

  render() {
    const btnClass = this.state.isHovered ? "pulse animated" : "";

    return (
      <div>
        <button className={btnClass} onMouseEnter={this.state.handleHover} onMouseLeave={this.state.handleHover}>Test</button>
      </div>
    );
  }
}

export default App;

2 Answers 2

62

You can use the onMouseEnter and onMouseLeave events on the component and toggle the class accordingly.

constructor(){
    super();
    this.state = {
        isHovered: false
    };
    this.handleHover = this.handleHover.bind(this);
}
handleHover(){
    this.setState(prevState => ({
        isHovered: !prevState.isHovered
    }));
}
render(){
    const btnClass = this.state.isHovered ? "pulse animated" : "";
    return <button className={btnClass} onMouseEnter={this.handleHover} onMouseLeave={this.handleHover}></button>
}

Update 05/07/19: Hooks

import React, { useState } from 'react';

export default function Component () {
  const [hovered, setHovered] = useState(false);
  const toggleHover = () => setHovered(!hovered);
  return (
    <button 
      className={hovered ? 'pulse animated' : ''}
      onMouseEnter={toggleHover}
      onMouseLeave={toggleHover}
    >
    </button>
  )
}
Sign up to request clarification or add additional context in comments.

14 Comments

i got an error message when i hovered over TypeError: Cannot read property 'setState' of null Specifically this line > 34 | this.setState({
Yeah, you'll need to bind that function to your component, check out the docs for more info. But basically you can either do this.handleHover = this.handleHover.bind(this) in the constructor, or change the props to onMouseEnter={this.handleHover.bind(this)} and onMouseLeave={this.handleHover.bind(this)}.
I made an assumption on your project since you didn't post any example code. If you post the entire component it would be easier to help.
sorry about that, i had added a state to my constructor but still doesn't seem to fix it, i added the code to my original question.
@tonejac It is passed in as an argument when providing a function as the first argument to this.setState: reactjs.org/docs/react-component.html#setstate
|
4

What about using the css :hover property? This worked way better for me by changing my hover class section in the css file to use :hover instead of react.

I tried using the above suggestions but react didn't seem fast enough to get it so the state would become wrong if the mouse moved slowly over the button.

2 Comments

I think this answer is simpler and efficient
The reason is I needed to modify the component ClassList or any property and simply put, css is limited in that. I could have gone in that direction, but it would have required a new planning. But yea I <3 :hover wherever I can

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.