A
A
Alexander2021-06-24 13:31:29
React
Alexander, 2021-06-24 13:31:29

How can I refactor this code?

export const Leftbar = () => {
  const [isOpen, setIsOpen] = React.useState<'kids' | 'women' | 'men' | undefined>()
  const onMouseKidsEnter = () => {
    setIsOpen('kids')
  }

  const onMouseWomenEnter = () => {
    setIsOpen('women')
  }

  const onMouseMenEnter = () => {
    setIsOpen('men')
  }

  const onMouseLeave = () => {
    setIsOpen(undefined)
  }

  return (
    <React.Fragment>
      <LeftBar>
        <LeftBarItem onMouseEnter={onMouseKidsEnter} onMouseLeave={onMouseLeave} isOpen={isOpen === 'kids'}>
          <span>Kids</span>
          <IoIosArrowBack />
        </LeftBarItem>
        <LeftBarItem onMouseEnter={onMouseWomenEnter} onMouseLeave={onMouseLeave} isOpen={isOpen === 'women'}>
          <span>Women</span>
          <IoIosArrowBack />
        </LeftBarItem>
        <LeftBarItem onMouseEnter={onMouseMenEnter} onMouseLeave={onMouseLeave} isOpen={isOpen === 'men'}>
          <span>Men</span>
          <IoIosArrowBack />
        </LeftBarItem>
      </LeftBar>

      <LeftbarDropdown
        activeMenu={isOpen === 'kids'}
        arrLinks={KidsLinks}
        onMouseEnter={onMouseKidsEnter}
        onMouseLeave={onMouseLeave}
        dropdownImg={dropdownKidsBg}
      />
      <LeftbarDropdown
        activeMenu={isOpen === 'women'}
        arrLinks={WomenLinks}
        onMouseEnter={onMouseWomenEnter}
        onMouseLeave={onMouseLeave}
        dropdownImg={dropdownWomenBg}
      />
      <LeftbarDropdown
        activeMenu={isOpen === 'men'}
        arrLinks={MenLinks}
        onMouseEnter={onMouseMenEnter}
        onMouseLeave={onMouseLeave}
        dropdownImg={dropdownMenBg}
      />
    </React.Fragment>
  )
}

interface LeftbarDropdownProps {
  activeMenu: boolean | undefined
  arrLinks: Array<HeroLinks>
  onMouseLeave: () => void
  onMouseEnter: () => void
  dropdownImg: string
}

const LeftbarDropdown: React.FC<LeftbarDropdownProps> = ({
  activeMenu,
  onMouseEnter,
  onMouseLeave,
  arrLinks,
  dropdownImg,
}): ReactElement => {
  return (
    <CSSTransition in={activeMenu} unmountOnExit timeout={500} classNames='dropdown'>
      <DropdownMenu onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} dropdownImg={dropdownImg}>
        {arrLinks.map((item, inx) => {
          return (
            <DropdownMenuLi>
              <DropdownMenuLink key={`dropdown-link-${inx}`} to={`${item.path}`}>
                {item.title}
              </DropdownMenuLink>
            </DropdownMenuLi>
          )
        })}
      </DropdownMenu>
    </CSSTransition>
  )
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
n1ksON, 2021-06-24
@axrising

- const onMouseKidsEnter = () => {
-    setIsOpen('kids')
-  }
-  const onMouseWomenEnter = () => {
-    setIsOpen('women')
-  }
-  const onMouseMenEnter = () => {
-    setIsOpen('men')
-  }
+ const onMouseParamEnter = (param) => {
+  setIsOpen(param);
+ }

<LeftBar>
-       <LeftBarItem onMouseEnter={onMouseKidsEnter} onMouseLeave={onMouseLeave} isOpen={isOpen === 'kids'}>
+ <LeftBarItem onMouseEnter={() => onMouseParamEnter('kids')} onMouseLeave={onMouseLeave} isOpen={isOpen === 'kids'}>
          <span>Kids</span>
          <IoIosArrowBack />
        </LeftBarItem>
-       <LeftBarItem onMouseEnter={onMouseWomenEnter} onMouseLeave={onMouseLeave} isOpen={isOpen === 'women'}>
+ <LeftBarItem onMouseEnter={() => onMouseParamEnter('women')} onMouseLeave={onMouseLeave} isOpen={isOpen === 'women'}>
          <span>Women</span>
          <IoIosArrowBack />
        </LeftBarItem>
-        <LeftBarItem onMouseEnter={onMouseMenEnter} onMouseLeave={onMouseLeave} isOpen={isOpen === 'men'}>
+ <LeftBarItem onMouseEnter={() => onMouseParamEnter('men')} onMouseLeave={onMouseLeave} isOpen={isOpen === 'men'}>
          <span>Men</span>
          <IoIosArrowBack />
        </LeftBarItem>
      </LeftBar>

Better yet, <LeftBarItem>put it in a separate component and pass the necessary props there with the kids/women/men parameter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question