How to create collapse expand list in react

后端 未结 2 1260
春和景丽
春和景丽 2021-01-17 03:17

I try to create a collapse and expand side menu in React (v 16.5) with the following criteria -

On page load first item (Circulars) will be in expanded

2条回答
  •  Happy的楠姐
    2021-01-17 03:33

    To do that I would use React.useState, since its a small state to control and to animate I would use CSS:

    The component would look like this:

    function SidebarNavs() {
      const [activeItem, setActiveItem] = React.useState(1);
    
      return (
        
    Sidebar Content Here Sidebar Content Here Work Orders
    ); } function SidebarItem({ title, children, setActiveItem, activeItem, index }) { const expanded = activeItem === index; const cls = "sidebar-nav-menu-item " + (expanded ? "item-active" : ""); return (
    {title}