I’m trying to make a dropdown navigation. The idea is to only have one active dropdown when the navigation is clicked and the others close. At the moment it works by the following concept I have but I’m stuck currently where both dropdown is open at the same time when both are clicked.
How can I toggle/add only have one active state and turn the other’s separate function state to false ? please help..
export default function Header(args) {
const [isActive, setActive] = useState(false);
const [isResourceActive, setResourceActive] = useState(false);
const handleToggle = () => {setActive(!isActive);};
const resourcesHandleToggle = () => {setResourceActive(!isResourceActive);};
return (
<NavItem onClick={handleToggle} className={isActive ? "active" : ""}></NavItem>
<NavItem onClick={resourcesHandleToggle} className={isResourceActive ? "active" :""}</NavItem>
<div className={isActive ? "active": ""}>DROPDOWN_1</div>
<div className={isResourceActive ?"active": ""}>DROPDOWN_2</div>
)
2
Answers
just force the false for the other dropp (or check it first with a "if" if u want)