简体   繁体   中英

React JS - Multi level dropdown menu

I have this example . My question is simple: how can I have only one submenu opened at a time?

eg: When the "menu 3" is opened I want "menu 2" to be closed..

Instead of an array for activeMenus state, use a single value, which can be null if no menus are open. (And change the name to activeMenu .)

In handleArrowClick , set activeMenu to null if it's already menuName , or menuName otherwise.

setActiveMenu(activeMenu === menuName? null: menuName)

When rendering SubMenu , toggle={activeMenu === menuName}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM