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

178
Visualizações
Link going to last scroll position instead of anchor

On my React app, I have two pages: page A, the home page, and page B.
On page B, I have links that go to page A in a specific section.

On page A: <section id="foo">Foo</section>
On page B: <a href="/#foo">Go to foo</a>

Every time I go to page B from page A and click on the link, the screen goes to the last scroll position and not the section.

I tried to deactivate the browser scroll history function with:

useEffect(() => {
  if (history.scrollRestoration) {
    history.scrollRestoration = 'manual';
  }
}, []);

I also to tried to add function to go to desired section:

useEffect(() => {
  if (window.location.hash) {
      const anchor = document.querySelector(window.location.hash);
      anchor.scrollIntoView();
  }
}, []);

But none of these solutions works. I reproduce the same behaviour on Chrome and Firefox.

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

0

It was a Gatsby feature: it overrides the browser default scroll history behaviour. More info there https://www.gatsbyjs.com/docs/how-to/routing/scroll-restoration/

I fixed it by adding:

// gatsby-browser.js
exports.shouldUpdateScroll = ({
  routerProps: { location },
  getSavedScrollPosition,
}) => {
  return location.href.indexOf("#") > -1 ? false : true;
};

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