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

242
Visualizações
How to access all the DOM element in React that got rendered through map?
return <div>
        <div >{'Audios'}</div>
        {urls.map(url => <div >
            <audio controls src={url} />
        </div>)}
    </div>;

I want the reference of all the audio tags.

We generally use ref={} for a single DOM element.

How can I use ref for all the elements of a map?

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

0

Create a ref as an initial value with array.

const urlsRef = useRef([]);

Then in the useEffect try to initialize the array length with urls length.

 useEffect(() => {
   urlsRef.current = urlsRef.current.slice(0, urls.length);
}, [urls]);

Then in the ref attribute push inside urlsRef

return <div>
    <div >{'Audios'}</div>
    {urls.map(url => <div >
        <audio controls src={url} ref={el => urlsRef.current[i] = el}  />
    </div>)}
</div>;
about 4 years ago · Juan Pablo Isaza Relatório

0

Depending on performance concerns if your lists are very large, you could use an id on the element and a DOM query to select it. Personally I would prefer to have a more specific ID, than an array index, incase of edge cases like reordering the list. Usually it's best to reference something that is in the data.

So if we have a list of URLs (['google.com', 'amazon.com', 'microsoft.com']), then we can update that to store an id as well. This should be done when you go to store the data, not in the render of a React Component.

urls: [{url: 'google.com', id: 1}, {url: 'amazon.com', id: 2}, {url: 'microsoft.com', id: 3}]

Now ideally you would have something like uuid() or a helper that uses a timestamp or something better than this example when generating an ID, but it is an example :)

With this in mind let's render things a little differently.

return (
  <div>
    <div>Audios</div>
    {urls.map(({url, id}) => (
      <div>
        <audio controls src={url} id={`audio-${id}`} />
      </div>
    ))}
  </div>
);

if you ever need to access one of these elements, lets say on a user click: onClick={handleClick(id)}

handleClick = (id) => (e) => {
  const elem = document.getElementById(`audio-${id}`)
  // do something with the audio element
}
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