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

173
Visualizações
How to use getElementsByClassName on dynamic html

What I'm trying to do is to get HTML tag by className on dynamic HTML that I fetched, but it returns undefined. It works if I try to getElementByClassName("main-page") because that class isn't dynamic

const HomePage = () => {
    const [pageData, setPageData] = useContext(PageContext)

    useEffect(() => {
        const allImages = document.getElementsByClassName("wp-block-column")
        console.log([...allImages])
    }, [])

    //render fronpage
    const renderMainPage = () => {
        //map the data and check for the site url (www.siteurl.com = front page)
        if (pageData) {
            return pageData.map(page => {
                if (window.location.origin + "/" === page.link) {
                    return <section dangerouslySetInnerHTML={{ __html: page.content.rendered }}></section>
                }
            })
        }
    }

    return (
        <h1 className="main-page"">{renderMainPage()}</h1>
    )
}

export default HomePage
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

From what I see the problem in pageData in context, probably your default value to context's pageData is false and renderMainPage() is not going further then first "if" statement

But its not preferred to use document selectors, use refs in React instead. Also from naming I see that you are trying to get images and not dom nodes, but with this logic you are going to get dom nodes. I'm sure there is better way/flow to access images you need than looking for them in nodes.

about 4 years ago · Juan Pablo Isaza Relatório

0

const elementsRef = useRef(data.map(() => createRef())); you can you dynamic create ref to access or create random iniNumber Array then assing to you elements when select them

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use this function that useLayoutEffect, you can modify your code like this,

useLayoutEffect(()=> {
   document.getElementsByClassName("yourclassname")
})
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