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

128
Visualizações
Browserrouting only running when displaying App

I build a website in react with Browserrouting and have the following code & issue

 const root = ReactDOM.createRoot(document.getElementById('root'));
 root.render(
<React.StrictMode>
    <BrowserRouter>
        <Routes>
            <Route path="/" element={<App/>}>
                <Route path="/home" element={<Homepage />} /> 

The Problem I have is when i run the App it opens the "App" and not the actual Homepage. I tried to fix it by changing change the App to Homepage but it is not working. In my understanding / needs to be there so the App displays in any other Path and is functioning.

Can some explain me, how i can make the path / still the Homepage without having to type /home to get there?

Thank you !

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

0

It appears you are rendering the Homepage in a nested route that is rendering App. In this configuration <Route path="/" element={<App />}> is what is called a Layout Route. App will be rendered when the path is exactly "/" and should render an Outlet component for the nested routes to render their content to.

An issue I see here, and you should have been seeing a react-router-dom invariant error regarding nesting an absolute path within a route rendering on an absolute path. In other words, "/home" is an absolute path and can't be nested under the absolute path "/"; it's not reachable.

If App really is a layout route and you always want it to render then you could remove the path prop from the parent route rendering App, and move Homepage to "/".

<BrowserRouter>
  <Routes>
    <Route element={<App />}>
      <Route path="/" element={<Homepage />} />
    </Route>
  </Routes>
</BowserRouter>

or convert Homepage into an index route and leave the "/" path on the parent layout route so the relative routing can still work properly and Homepage is rendered on "/".

<BrowserRouter>
  <Routes>
    <Route path="/" element={<App />}>
      <Route index element={<Homepage />} />
    </Route>
  </Routes>
</BowserRouter>

I should point out that this is a slightly abnormal configuration. Typically the App component would render the Routes and Route components instead of an Outlet and it's assumed to all render on "/" by default. The Homepage component would then be rendered on path="/".

Example:

<BrowserRouter>
  <App />
</BowserRouter>

...

const App = () => {
  .... app logic ....

  return (
    <Routes>
      <Route path="/" element={<Homepage />} />
    </Routes>
  );
};

There is OFC, the chance here that you didn't intend to nest these routes and want them to be matched and rendered independently. In this case the solution is to just unnest the Homepage route and continue using absolute paths for the root routes.

Example:

<BrowserRouter>
  <Routes>
    <Route path="/" element={<App />} />
    <Route path="/home" element={<Homepage />} />
  </Routes>
</BowserRouter>
about 4 years ago · Juan Pablo Isaza Relatório

0

I found the issue

I had to

 <Route path="" element={<App/>}>
            <Route path="/" element={<Homepage />} /> 

Delete the Path / in App and only add Path / in Homepage.

It works now

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