Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
Map function is not working properly when I hit onClick

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>
  );
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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>;
          })
        : ""}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda