Home > OS >  Map function is not working properly when I hit onClick
Map function is not working properly when I hit onClick

Time:12-29

So I am making this project in ReactJs, which has a sidebar, where I am trying to implement dropdown menu.
Required Behavior
If I click in any of the option of the sidebar, if it has a submenu, it will show. And close upon again clicking.
Current Behavior
If I click any of the options, all the submenus are showing at once. For example if I click publications option, it shows me all the options, such as featured publications, journal publications. How do I fix that?

My sidebarItems array

const sidebarItems = [
  {
    title: "Publications",
    url: "#",
    subMenu: [
      {
        title: "Journal Publications",
        url: "#",
      },
      {
        title: "Featured Publications",
        url: "#",
      },
    ],
  },
  {
    title: "Team Members",
    url: "#",
    subMenu: [
      {
        title: "Current Members",
        url: "#",
      },
      {
        title: "Lab Alumni",
        url: "#",
      },
    ],
  },

  {
    title: "Projects",
    url: "#",
    subMenu: [
      {
        title: "Project 1",
        url: "#",
      },
      {
        title: "Project 2",
        url: "#",
      },
      {
        title: "Project 3",
        url: "#",
      },
    ],
  },
  {
    title: "News",
    url: "#",
  },
  {
    title: "Contact Us",
    url: "#",
  },
];

export default sidebarItems;

The Sidebar Component

import { useState } from "react";
import { Box, Text } from "@chakra-ui/react";
import sidebarItems from "./sidebarItems";



export default function Sidebar() {
  const [isOpen, setIsOpen] = useState(false);

  return (
    <div>
      <Box>
        {sidebarItems.map((items) => {
          return (
            <Box
              width='200px'
              height='40px'
              textAlign='center'
              cursor='pointer'
              onClick={() => {
                setIsOpen(!isOpen);
              }}
            >
              <Text>{items.title}</Text>
              {isOpen
                ? items.subMenu?.map((item) => {
                    return <Text>{item.title}</Text>;
                  })
                : ""}
            </Box>
          );
        })}
      </Box>
    </div>
  );
}

CodePudding user response:

You have to use an array state variable. Your single state variable isOpen is dictating all the subMenus here:

           {isOpen
                ? items.subMenu?.map((item) => {
                    return <Text>{item.title}</Text>;
                  })
                : ""}

You need to have an array state variable here, so each sidebar item has a corresponding boolean to dictate opening/closing of value.

const [isOpen, setIsOpen] = useState(Array(sidebarItems.length).fill(false));

Now you have to ensure that you are setting it correctly and manipulating the right array element.

onClick={() => {
                let newIsOpen = [...isOpen];
                newIsOpen[index] = !isOpen[index];
                setIsOpen(newIsOpen);
              }}

I hope this helps you reach your solution

CodePudding user response:

This is happening because you are using wrong logic and you don't specify which submenu should be shown. First, delete the current state and dont use it. Then, you should define another state like:

const [selectedMenu, setSelectedMenu] = useState("");

then, define this function:

const handleClick = (title) => {
    setSelectedMenu(title); 
}

after that, once you click on Box, you should invoke function like this:

 onClick={() => handleClick(item.title)}

consequently, you should write your logic like this:

      <Text>{items.title}</Text>
      {item.title === selectedMenu
        ? items.subMenu?.map((item) => {
            return <Text>{item.title}</Text>;
          })
        : ""}

CodePudding user response:

I think the problem is occurring because you have only 1 state variable set for every sidebar option. Every sidebar option should have its own variable keeping track of whether its submenu should open or not.

In your code, when the isOpen state variable is set to true then when the function maps over all the options the variable's value will always be true.

Try setting a variable for each of the menu options which contains whether the submenu should open or not.

  • Related