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

186
Visualizações
React Link element causing the app to fail

I just started to build this shopping cart project. and I am able to display the components fine when I manually change the url but -- When I add the Link element to the Nav my app stops working. Why is that happening?

I want to the display the different components when they are clicked.

When I add the Link Element to the navigation bar I get the following errors in the console:

The above error occurred in the <Link> component

Uncaught Error: useHref() may be used only in the context of a <Router> component.

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

function Nav () {
    return (
        <nav>
            
            <h3>Logo</h3>
            <ul>
                <Link to="/home">
                    <li>Home</li>
                </Link>
                <Link to="/shop">
                    <li>Shop</li>
                </Link>
                <Link to="/cart">
                    <li>Cart</li>
                </Link>
            </ul>
        </nav>
    )
}

export default Nav;
import React from 'react';
import {BrowserRouter, Route, Routes} from 'react-router-dom';
import Home from './components/main/Home'
import Cart from './components/main/Cart'
import Shop from './components/main/Shop'
import About from './components/main/About'

function RouteSwitch() {
  return (
    <BrowserRouter>
      <Routes>
          <Route path='/' exact element={<Home />} />
          <Route path='/shop' element={<Shop />} />
          <Route path='/about' element={<About />} />
          <Route path='/cart' element={<Cart />} />
      </Routes>
    </BrowserRouter>
  );
}

export default RouteSwitch;
import './App.css';
import React from 'react';
import RouteSwitch from './RouteSwitch';
import Nav from './components/Nav'
import Footer from './components/Footer';

function App() {
  return (
    <div>
      <Nav/>
      <RouteSwitch />
      <Footer/>
    </div>
  );
}

export default App;
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Your nav component must be inside of Router component.

<BrowserRouter>
      <Nav /> // here
      <Routes>
          <Route path='/' exact element={<Home />} />
          <Route path='/shop' element={<Shop />} />
          <Route path='/about' element={<About />} />
          <Route path='/cart' element={<Cart />} />
      </Routes>
      <Footer />
</BrowserRouter>
about 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