trendmicro-frontend / react-sidenav

React SideNav component

Home Page:https://trendmicro-frontend.github.io/react-sidenav/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The onToggle callback does not work with react hooks setState.

jffin opened this issue · comments

commented

When I try to use SideNav setToggled callback with React hooks it fails with error "setState is not a function.

function SideNavMenu() {
  const {toggled, setToggled} = useState(false);

  const sideNavClasses = () => {
    return `${toggled ? 'toggled' : ''}`
  };

  return (
    <StyledSideNav
      className={sideNavClasses()}
      onToggle={expanded => setToggled(expanded)}
    >
      <StyledToggle/>
      <Nav defaultSelected={'home'}>
           ...
      </Nav>
    </StyledSideNav>
  );
}

export default SideNavMenu;

Why does it happen?

useState return an array not an object so you should have const [toggled, setToggled] = useState(false); instead of const {toggled, setToggled} = useState(false);

commented

Very stupid mistake (
Thanks a lot

No worries mate, this happens to all of us... Happy learning