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

228
Visualizações
React - Generating onClick from non component file

I have this code that defines structure of my navbar

const tabsStructure = [
    {name: "File",
        menu: [
            {
                name: "New",
                onClick: () => {/*Toggle component*/}
            },
            {name: "Save",
                menu: [
                    {
                        name: "Save to browser",
                        onClick: () => {/*Toggle component*/}
                    },
                    {
                        name: "Save JSON",
                        onClick: () => {/*Toggle component*/}
                    }
                ],
            },
            {
                name: "Load",
                onClick: () => {/*Toggle component*/}
            }
        ]
    },
    
    {name: "Edit",
        menu: [
            {
                name: "Resize",
                onClick: () => {/*Toggle component*/}
            }
        ]
    }
];

export default tabsStructure;

That array of objects is being imported to one of the React components which properly generates HTML structure. I would want to add some functions for toggling other components, to some of the menu items (as you can see in my code). I would do that through setting proper states of common parent of both, the menu item and the component I want to toggle. However, from the scope of this file I don't have access to other components, their states etc. My question is: what is the best way of dealing with it? Should I move tabsStructure to the state of App component? If so, would it be possible to still keep it in separate file?

Here's structure that my code generates: structure

For example, I would like to toggle DocumentCreator through clicking on "New" menu item.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The solution is either to hoist your state (doesn't need to be App.js, just somewhere, where every component affected can have access to it) or using custom events and reacting to them.

about 4 years ago · Santiago Trujillo 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