The onToggle callback does not work with react hooks setState.
jffin opened this issue · comments
jffin 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?
Ernst Pierre commented
useState return an array not an object so you should have const [toggled, setToggled] = useState(false);
instead of const {toggled, setToggled} = useState(false);
jffin commented
Very stupid mistake (
Thanks a lot
Ernst Pierre commented
No worries mate, this happens to all of us... Happy learning