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

98
Visualizações
Nothing shows up using react-router

I am new to React, and I cannot get my routes to work. I am following the tutorial in the documentation but somehow it does not work. The content inside the App component does not show up so I assume there is some kind of problem with BrowserRouter or Routes or my nesting but I really cannot figure out what's going on.

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter, Routes, Route } from "react-router-dom";
import App from './App';
import Products from './routes/products';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <BrowserRouter>
        <Routes>
            <Route path="/" element={<App />}>
                <Route path="products" element={<Products />} />
            </Route>
        </Routes>
    </BrowserRouter>
  </React.StrictMode>
);
routes/app.js

import { Outlet, Link } from "react-router-dom";

export default function App() {
  return (
      <div>
        <h1>Hello World</h1>
        <nav>
            <Link to="/products">Products</Link>
        </nav>
        <Outlet />
      </div>
  );
}
routes/products.jsx

export default function Products(){
    return (
        <div>
            <h1>Products</h1>
        </div>
    );
}

P.S. I already saw some stackoverflow answers but still I could not see what I am getting wrong.

EDIT: Thank you for the attention and support but after rewriting the code and playing around with it I found out that my App component does not render when I add the Link to="" component. Any Idea why this is happening?

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

0

Try this router structure:

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

0

I have made only one change to your code, i.e. I use a wildcard to redirect the user to App for each route that does not match the other routes (i.e. each route that does not match /products). You can add a wildcard with * like this: <Route path="*" element={<App />} />

index.tsx

import { StrictMode } from "react";
import * as ReactDOMClient from "react-dom/client";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { Products } from "./Products";

import App from "./App";

const rootElement = document.getElementById("root");
const root = ReactDOMClient.createRoot(rootElement);

root.render(
  <StrictMode>
    <BrowserRouter>
      <Routes>
        <Route path="*" element={<App />} />
        <Route path="products" element={<Products />} />
      </Routes>
    </BrowserRouter>
  </StrictMode>
);

App.tsx

import "./styles.css";
import { Link } from "react-router-dom";

export default function App() {
  return (
    <>
      <Link to="/home">HOME</Link>
      <Link to="/products">PRODUCTS</Link>
      <div>HOME PAGE HERE</div>
    </>
  );
}

Products.tsx

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

export const Products = () => {
  return (
    <>
      <Link to="/home">HOME</Link>
      <Link to="/products">PRODUCTS</Link>
      <div>PRODUCTS PAGE HERE</div>
    </>
  );
};

You can checkout the example in codesandbox. Click on the links to navigate between the pages.

about 4 years ago · Juan Pablo Isaza Relatório

0

I am here answering my own question. After 2 days of madness I found out that I had two package.json files, so I just reinstalled locally react-router and eventually it worked. I came to this conclusion thanks to another answer here on StackOverflow.

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