0

I'm working on a group of divs that expand a specific div on hover, my problem is when I hover on an element all the divs is affected.

I dont know why its happening,

Hope you understand me.

Thanks

Sample Code

constructor(props) {
    super(props);
    this.state = {
      isHovered: false
    };
    this.handleMenuHover = this.handleMenuHover.bind(this);
  }

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

  render() {
    const menuActive = this.state.isHovered ? "active" : "";
    return (
      <div className="slider-menu">
        <div
          className={"menu " + menuActive}
          onMouseEnter={this.handleMenuHover}
          onMouseLeave={this.handleMenuHover}
        >
          a
        </div>
        <div
          className={"menu " + menuActive}
          onMouseEnter={this.handleMenuHover}
          onMouseLeave={this.handleMenuHover}
        >
          b
        </div>
        <div
          className={"menu " + menuActive}
          onMouseEnter={this.handleMenuHover}
          onMouseLeave={this.handleMenuHover}
        >
          c
        </div>
        <div
          className={"menu " + menuActive}
          onMouseEnter={this.handleMenuHover}
          onMouseLeave={this.handleMenuHover}
        >
          d
        </div>
      </div>
    );
  }
3
  • You need to keep track of the hover states of the individual divs, and change the class accordingly. The way you have it now, you're using the same hover state for all classes. Commented May 2, 2018 at 10:42
  • I thought, I can do it just like jQuery.. thank you sir Commented May 2, 2018 at 10:45
  • so I need to create multiple hover events to achieve this.. Commented May 2, 2018 at 10:48

1 Answer 1

2

Make a single div component with event handlers, then populate it. Example:

const Home = () => (
  <div>
    <Menu>a</Menu>
    <Menu>b</Menu>
    <Menu>c</Menu>
    <Menu>d</Menu>
  </div>
)

class Menu extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isHovered: false
    };
    this.handleMenuHover = this.handleMenuHover.bind(this);
  }

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

  render() {
    const menuActive = this.state.isHovered ? "active" : "";
    return (
      <div className="slider-menu">
        <div
          className={"menu " + menuActive}
          onMouseEnter={this.handleMenuHover}
          onMouseLeave={this.handleMenuHover}
        >
          {this.props.children}
        </div>
      </div>
    );
  }
}

export default Home;
Sign up to request clarification or add additional context in comments.

2 Comments

it is working fine in the link but for me it adds class to all elements even if i am binding this with event handler.
The problem is that state must refer to only one of the elements, not all, hence having a component for each of the <div>s guarantees the single mapping between state and elements.

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.