Inline CSS styles in React: how to implement a:hover?

前端 未结 18 902
星月不相逢
星月不相逢 2020-11-28 21:51

I quite like the inline CSS pattern in React and decided to use it.

However, you can\'t use the :hover and similar selectors. So what\'s the best way to

18条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 22:03

    With a using of the hooks:

    const useFade = () => {
      const [ fade, setFade ] = useState(false);
    
      const onMouseEnter = () => {
        setFade(true);
      };
    
      const onMouseLeave = () => {
        setFade(false);
      };
    
      const fadeStyle = !fade ? {
        opacity: 1, transition: 'all .2s ease-in-out',
      } : {
        opacity: .5, transition: 'all .2s ease-in-out',
      };
    
      return { fadeStyle, onMouseEnter, onMouseLeave };
    };
    
    const ListItem = ({ style }) => {
      const { fadeStyle, ...fadeProps } = useFade();
    
      return (
        
          {...}
        
      );
    };
    

提交回复
热议问题