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

161
Visualizações
setTimeout function does not display HTML after function ends

I have created a timeout function for 3 seconds and after that time it is supposed to render the HTML. I'm following the official documentation to do this and the code is like on the documentation. Anyone has had a similar issue?

render() {
return (
<div>
    {this.state.activePage === 'Home' ? setTimeout(function () {
        <p>Hello World</p>
    }, 3000) : null}
</div>
)
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You should not write code like that in react. The setTimeout returns:

The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout(). T

and not the JSX which you returned from callback. But even if it did asynchronously return the JSX which you return from callback, you won't see the new content on the screen. To change UI in react you must change a state variable using setState which will cause a re render. Also render is not the place to call setState. You can call setState in useEffect for example.

about 4 years ago · Juan Pablo Isaza Relatório

0

It is not possible to return a value from the function passed to setTimeout.

Here are two options you could try

  1. Create a state field to store the message you want to display and update the state from inside of the setTimeout. Also such an update cannot be done inside of the return. You would want to move this logic to a component mount function.
  2. Here is a more vanilla JS approach to this issue using promise objects Get return value from setTimeout

Also could you provide the document you are referring to for this code, so we can have a deeper dive onto the issue?

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