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

198
Visualizações
How to refresh page when web page is rotated?

I'm using Next JSs, I'm having trouble finding a solution how to refresh the page when it's rotated

const app () => {

  useEffect(()=>{
  window.addEventListener("orientationchange", function() {
    window.location.reload()
  });})

   return(<>some code</>);
}


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

0

I would strongly recommend you don't do that.

But there are a couple of problems with your implementation of it:

  1. To hook up an event listener on component mount, you need an empty dependency array on your useEffect. Otherwise, your effect callback is called on every render which, in your case, will add multiple handlers.

  2. You aren't removing the handler when the component is unmounted. You should return a cleanup callback from the useEffect callback.

  3. The orientationchange event is deprecated and has poor browser support. Instead, listen to change on Screen.orientation which has better browser support. Alternatively, just listen for resize, since a change in screen orientation that you care about will trigger a resize event, and mobile devices are getting more and more sophisticated with how the screen real estate is used (split screen, etc.). It's possible not all of those would cause a change in orientation, but you seem to care about a change in size.

Separately, app isn't a valid component name. Component functions must start with a capital letter.

Fixing all of those:

const App () => {
    useEffect(()=>{
        const handler = () => {
            // Handle the change in orientation here (I recommend **not** reloading)
        };
        Screen.orientation.addEventListener("change", handler); // Or window resize
        return () => {
            // On unmount, remove the handler
            Screen.orientation.removeEventListener("change", handler); // Or window resize
        };
    }, []);
    // ^^−−−− tells `useEffect` to only call your callback once, when the component
    // is first mounted.

    return (<>some code</>);
}
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