在React中如何使只有一个活动状态显示下拉菜单,其他的保持隐藏

huangapple go评论66阅读模式
英文:

How to have only one active state showing dropdown and others false in react

问题

以下是您要翻译的代码部分:

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>
    )
}

希望这对您有所帮助。

英文:

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 = () =&gt; {setActive(!isActive);};
    const resourcesHandleToggle = () =&gt; {setResourceActive(!isResourceActive);};

         return (
    &lt;NavItem onClick={handleToggle} className={isActive ? &quot;active&quot; : &quot;&quot;}&gt;&lt;/NavItem&gt;  
    &lt;NavItem onClick={resourcesHandleToggle} className={isResourceActive ? &quot;active&quot; :&quot;&quot;}&lt;/NavItem&gt;
        
        
        &lt;div className={isActive ? &quot;active&quot;: &quot;&quot;}&gt;DROPDOWN_1&lt;/div&gt;
        &lt;div className={isResourceActive ?&quot;active&quot;: &quot;&quot;}&gt;DROPDOWN_2&lt;/div&gt;
    )

答案1

得分: 0

const [active, setActive] = useState(undefined);
const toggle = (name) => {
  if (active === name) {
    setActive(undefined)
  } else {
    setActive(name)
  }
}
&lt;NavItem onClick={() => toggle('DROPDOWN_1')} className={active === 'DROPDOWN_1' ? "active" : ""}></NavItem>  
&lt;NavItem onClick={() => toggle('DROPDOWN_2')} className={active === 'DROPDOWN_2' ? "active" : ""}></NavItem>
&lt;div className={active === 'DROPDOWN_1' ? "active" : ""}>DROPDOWN_1</div>
&lt;div className={active === 'DROPDOWN_2' ? "active" : ""}>DROPDOWN_2</div>
英文:
const [active, setActive] = useState(undefined);
const toggle = (name) =&gt; {
  if (active === name) {
    setActive(undefined)
  } else {
    setActive(name)
  }
}
&lt;NavItem onClick={() =&gt; toggle(&#39;DROPDOWN_1&#39;)} className={active === &#39;DROPDOWN_1&#39; ? &quot;active&quot; : &quot;&quot;}&gt;&lt;/NavItem&gt;  
&lt;NavItem onClick={() =&gt; toggle(&#39;DROPDOWN_2&#39;)} className={active === &#39;DROPDOWN_2&#39; ? &quot;active&quot; :&quot;&quot;}&lt;/NavItem&gt;
&lt;div className={active === &#39;DROPDOWN_1&#39; ? &quot;active&quot;: &quot;&quot;}&gt;DROPDOWN_1&lt;/div&gt;
&lt;div className={active === &#39;DROPDOWN_2&#39; ? &quot;active&quot;: &quot;&quot;}&gt;DROPDOWN_2&lt;/div&gt;

答案2

得分: -1

只强制其他放置物的假(或者如果您想要的话,首先用 "if" 进行检查)

const handleToggle = () => {
    setActive(!isActive);
    setResourceActive(false);
};

const resourcesHandleToggle = () => {
    setResourceActive(!isResourceActive);
    setActive(false);
};
英文:

just force the false for the other dropp (or check it first with a "if" if u want)

const handleToggle = () =&gt; {    
    setActive(!isActive);
    setResourceActive(false);    
};
   

 const resourcesHandleToggle = () =&gt; {
    setResourceActive(!isResourceActive);
    setActive(false);    
};

huangapple
  • 本文由 发表于 2023年2月18日 02:15:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定