0

I am trying to toggle class in one of my react component. The idea is to add class when the mouse enter and to remove the class when the mouse leave only in the element only the element in where the user perform the actions. However this is not the case as when the action is being performed all the element with the function are behaving equally.

This is my code so far:

export default class Projects extends Component{
  constructor(){
    super();
    this.state = {
      isHovered : false
    }
  }
  //handle mouse enter
  handleMouseEnter = () =>{
    this.setState({
      isHovered : true
    });
  }
  //handle mouse leave
  handleMouseLeave = () =>{
    this.setState({
      isHovered : false
    });
  }
//render component
  render(){
    let display = "";
    if(this.state.isHovered === true){
      display = "active";
    }else{
      display = "disable";
    }

    return(
      <section className="projects">
        {/*section project wrapper*/}
        <div className="p-wrapper">
            <h1 className="title">Projects</h1>
            <hr/>
            {/*projet wrapper*/}
            <div className="projects-wrapper">
              {/*project item wrapper*/}
              <div className="project-item" onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>{/*FMA Web development*/}
                <div className={"p-description " + display}>
                  <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure quos dolorem, ipsa eaque minima saepe fugit hic libero recusandae! Obcaecati esse odit id incidunt vitae aperiam dicta atque blanditiis sint?</p>
                </div>
                <div className="p-image">
                  <img src="asset/img/fma_web.png" alt="FMA Web Development"/>
                </div>
              </div>
              <div className="project-item" onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>{/*Web development using php*/}
                <div className={"p-description " + display}>
                  <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure quos dolorem, ipsa eaque minima saepe fugit hic libero recusandae! Obcaecati esse odit id incidunt vitae aperiam dicta atque blanditiis sint?</p>
                </div>
                <div className="p-image">
                  <img src="asset/img/web_dev_php.png" alt="FMA Web Development Using PHP"/>
                </div>
              </div>
              <div className="project-item" onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>{/*Movie Catalog*/}
                <div className={"p-description " + display}>
                  <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure quos dolorem, ipsa eaque minima saepe fugit hic libero recusandae! Obcaecati esse odit id incidunt vitae aperiam dicta atque blanditiis sint?</p>
                </div>
                <div className="p-image">
                  <img src="asset/img/movie_catalog.png" alt="Movie Catalog"/>
                </div>
              </div>
            </div>
        </div>
      </section>
    );
  }
}

I have read the use of key in the documentation and read other question especially this one LINK,however when I try to implement I do not get the desired result.

===========================

EDIT

This is what I get now. enter image description here

As you can see when I hover both of the element triggers.

This is my CSS Code:

/*Projects Start*/
.projects{
  width: 100%;
}

.p-wrapper{
  margin: 0 auto;
  width: 90%;
  height: 100%;
}
.projects-wrapper{
  margin-top: 2rem;
  width: 100%;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
}
.project-item{
  margin: 1rem;
  width: 30%;
  position: relative;
  box-shadow: 2px 3px 37px -5px rgba(0,0,0,0.84);
}
.p-description{
  position: absolute;
  height: 100%;
  width: 100%;
  background-color: rgba(43, 40, 40, 0.61);
  color: white;
}
.p-description p {
  margin: 1rem;
}
.p-title{
  margin: 1rem;
}
.active{
  display: block;
  transition: all 2s ease-in;
}
.disable {
  display: none;
}
9
  • Look at how the functions are bound in the link you reference. You're setting and referencing state for the whole Projects component. Instead you want to either be specifying which element is hovered (the answer in your link) or using another component for each project-item that has it's own hovered state (the further comment by blex). Commented Feb 25, 2018 at 21:07
  • if you just wants a different styling when mouse is hovered over the element why not use a css solution like .element:hover{...} ? Commented Feb 25, 2018 at 21:08
  • @Sagivb.g basically the class p-description is hidden (display : none) at the beginning of the rendering, it will be available shown (display : block) only when the user will hover. Commented Feb 25, 2018 at 21:16
  • with display:none you can't trigger mouse events Commented Feb 25, 2018 at 21:22
  • @Sagivb.g I am not triggering the element using css properties. I have included the css and a image for a better explanation of the issue Commented Feb 25, 2018 at 21:29

0

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.