Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!