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

171
Visualizações
¿Cómo obtener la referencia de un componente a través de la propagación de eventos de JavaScript?

tengo un codigo asi

 const dailyRef = createRef(); const monthlyRef = createRef(); const annuallyRef = createRef(); const handleBillingCycleClick = ({target}) => { const value = target.dataSet.value // assign an attribute value (backgroundColor) to the clicked button // how to know the clicked button ref here? // I want to have access to the clicked button ref, so I can do something like this: // CLICKED-BUTTON-REF.current.variant = "contained" .ie. <Button variant='contained'> } <FormControl> <Typography variant="overline" sx={{ fontWeight: 'fontWeightBold' }}> Billing cycle </Typography> <ButtonGroup variant="outlined" onClick={handleBillingCycleClick}> <Button ref={dailyRef} data-value="daily">Daily</Button> <Button ref={monthlyRef} data-value="monthly">Monthly</Button> <Button ref={annuallyRef} data-value="annually">Annually</Button> </ButtonGroup> </FormControl>

Estoy usando Material UI como una biblioteca de componentes, pero se aplicarán los mismos conceptos sin usarlo.

Intenté hacer lo siguiente pero, por supuesto, no funcionará sin eval() :

[${value}Ref].current.variant = 'contained'

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

0

Puede usar el objeto de la siguiente manera:

 const myRefs = { daily: createRef(), monthly: createRef(), annually: createRef() }; const handleBillingCycleClick = ({target}) => { const value = target.dataSet.value const reference = myRefs[value]; // now you can use the reference here } <FormControl> <Typography variant="overline" sx={{ fontWeight: 'fontWeightBold' }}> Billing cycle </Typography> <ButtonGroup variant="outlined" onClick={handleBillingCycleClick}> <Button ref={myRefs.daily} data-value="daily">Daily</Button> <Button ref={myRefs.monthly} data-value="monthly">Monthly</Button> <Button ref={myRefs.annually} data-value="annually">Annually</Button> </ButtonGroup> </FormControl>
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