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

98
Visualizações
Referencing a specific key in an object based on given variable

I have a component in a React application called ViewSwitcher that allows the user to switch between different views and thus render content based on that view. It looks like this:

enter image description here

I want to be able to reuse this component in a number of different parent components. So ViewSwitcher has a prop called component that tracks which parent component it is being rendered from. Every ViewSwitcher has specific buttons that are also set in the parent component through an array called buttons.

const buttons = ['Inbox', 'Sent', 'Drafts'];

<ViewSwitcher buttons={buttons} component='notifications' />

When a button is clicked, the UI is different (orange outline) vs when it is not clicked (grey outline). In order to determine which button is clicked, I have state in the ViewSwitcher component called localViews.

const [localViews, setLocalViews] = useState({
roster: 'board',
schedule: 'jobs',
jobManagement: 'table',
notifications: 'inbox',
});

I want to reference the value of specific keys in the localViews state to determine whether to render a ClickedButton or a ViewButton. So for example, in the initial state shown above, when ViewSwitcher is rendered from the notifications component, the 'inbox' button would render as a ClickedButton and the other two (i.e. 'sent' and 'drafts') as ViewButtons.

My question is how can I reference the relevant key in the localViews to determine which type of button to render? I tried the following but this is just throwing an error:

<ButtonGroup aria-label='outlined primary button group'>
{buttons.map((button) =>
    localViews[component].toLowerCase() === button.toLowerCase() ? (
    <ClickedButton>{button}</ClickedButton>
    ) : (
    <ViewButton>{button}</ViewButton>
    ),
)}
</ButtonGroup>
about 4 years ago · Juan Pablo Isaza
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