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

422
Visualizações
Async/Await a Page Reload in React TypeScript

I am having a button, and when I click on it, it should do the following things in order:

  • First refresh the Page
  • Then Open the Side Pane in React

This is my code snippet that is existing:

<button onClick={() => {
    refreshPage();
    setPaneIsOpen(true);
}}>Click me</button>

const refreshPage = () => {
    window.location.reload();
}

setPaneIsOpen is a State that when set to true, will open the side pane. I want to specifially first refresh the page and then set the Pane.

I have tried this async/await promise approach:

 function refreshPagePromise() {
      return new Promise<void>((resolve) => {
           window.location.reload();
           resolve();
      });
 }

 async function refreshAndOpen() {
      await refreshPagePromise();
      setIsPaneOpen(true);
 }

 <button onClick={() => { refreshAndOpen(); }}>Click me</button>

But I cannot seem to handle page reload. I tried to create a Code Sandbox, but since this is very complex, I could not. I will try to add one in the edits If I was able to reproduce.

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

0

One hacky way would be to use localStorage.

add this useEffect that runs on page load:

useEffect(() => {
    const shouldOpenPane = !!localStorage.getItem("setPaneOpen")
    if(shouldOpenPane){
        setIsPaneOpen(true);
        // Maybe the row beneath should not be there, 
        // Idk what behaviour u want if user reloads page manually
        localStorage.removeItem("setPaneOpen")
    }
}, []);

Update your button like this:

<button
      onClick={() => {
        localStorage.setItem("setPaneOpen", "true"); // can be set to any thruthy value
        refreshPage();
      }}
    >
      Click me
    </button>
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