Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda