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

87
Visualizações
React : navigate to a html tag inside a component

i want to go from a component to another and slide directly to an html tag inside of it .

using react router v6

<Route path="/mypage/#c" element = {<MyPage/>} />   

( #c is a id of the html tag in the /mypage )

but the code i've provided isn't working .

it just goes to the page and doesn't slide into the tag .

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The hash should be added to the link or simply read from the URL, i.e. the location object, instead specified on the Route component's path prop. Adding it to the path will break route path matching.

<Route path="/mypage" element={<MyPage />} />

react-router-dom@6 doesn't handle linking directly to hash-tags in the document, and at the moment the best library out there for doing this in previous versions of RRD, react-router-hash-link has yet to update to support RRDv6.

The MyPage component could use a useEffect hook with a dependency on the location.hash, query the DOM, and attempt to scroll that element into view.

Example:

import { useLocation } from "react-router-dom";

const MyPage = () => {
  const { hash } = useLocation();

  useEffect(() => {
    const el = document.querySelector(hash);
    if (el) {
      el.scrollIntoView({ behavior: "auto" });
    }
  }, [hash]);

  return (
    <>
      ... content ...
      <div id="c">This is the content I'm interested in</div>
      ... more content ...
    </>
  );
};

Edit react-navigate-to-a-html-tag-inside-a-component

almost 4 years ago · Santiago Trujillo 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