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

235
Visualizações
How to build a generic menu component?

I have to build a menu screen component that can be used later on easily. The component only has options in a list way.

    const options = [
  {
    text: 'Option 1',
  },
  {
    text: 'Option 2',
  },
  {
    text: 'Option 3',
  },
];

and I just map it inside the component.

const Menu = () => {
  return (
    <div>
      {options.map((option, index) => (
        <MenuOption text={option.text} index={index} />
      ))}
    </div>
  );
};
const MenuOption = ({ text, index }) => {
  return (
    <div>
      <p>{text}</p>
    </div>
  );
};

So anyone can use this menu component by passing in the options data. What i want is for this component to also run functions corresponding to the option clicked. So we can easily pass the function that needs to be run as a prop to the component. The main question is that how do i relate/map the options with the corresponding function?

NOTE: I can't send the functions with the options data object in the props.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I mean you can just send one callback to the Menu component:

const Menu = ({ onOptionClick }) => {
  return (
    <div>
      {options.map((option, index) => (
        <MenuOption text={option.text} index={index} onClick={onOptionClick} />
      ))}
    </div>
  );
};

const MenuOption = ({ text, index, onClick }) => {
  return (
    <div onClick={() => { onClick(index, text, whatever) }}>
      <p>{text}</p>
    </div>
  );
};
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