How can I access a hover state in reactjs?

前端 未结 4 705
清酒与你
清酒与你 2021-01-30 04:40

I have a sidenav with a bunch of basketball teams. So I would like to display something different for each team when one of them is being hovered over. Also, I am using Reactjs

4条回答
  •  旧时难觅i
    2021-01-30 05:33

    For having hover effect you can simply try this code

    import React from "react";
      import "./styles.css";
    
        export default function App() {
    
          function MouseOver(event) {
            event.target.style.background = 'red';
          }
          function MouseOut(event){
            event.target.style.background="";
          }
          return (
            
    ); }

    Or if you want to handle this situation using useState() hook then you can try this piece of code

    import React from "react";
    import "./styles.css";
    
    
    export default function App() {
       let [over,setOver]=React.useState(false);
    
       let buttonstyle={
        backgroundColor:''
      }
    
      if(over){
        buttonstyle.backgroundColor="green";
      }
      else{
        buttonstyle.backgroundColor='';
      }
    
      return (
        
    ); }

    Both of the above code will work for hover effect but first procedure is easier to write and understand

提交回复
热议问题