Guys i’m trying to work on my navigation bar and use route but it always shows errors
I don’t know the reason but it isn’t convinced with the routes at all and the error is
JavaScript
x
14
14
1
"index.tsx:19 Uncaught Error: useRoutes() may be used only in the context of a <Router>
2
component.
3
at invariant (index.tsx:19)
4
at useRoutes (index.tsx:637)
5
at Routes (index.tsx:339)
6
at renderWithHooks (react-dom.development.js:14985)
7
at mountIndeterminateComponent (react-dom.development.js:17811)
8
at beginWork (react-dom.development.js:19049)
9
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
10
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
11
at invokeGuardedCallback (react-dom.development.js:4056)
12
at beginWork$1 (react-dom.development.js:23964)
13
"
14
below is my app.jsx code
JavaScript
1
24
24
1
import React, { Component } from 'react';
2
import Home from './home';
3
import {Route,Routes} from "react-router-dom"
4
5
class App extends React.Component {
6
render() {
7
return (
8
<React.Fragment>
9
10
<Routes>
11
<Route path="/home" element={<Home />} />
12
</Routes>
13
14
15
16
</React.Fragment>
17
18
19
);
20
}
21
}
22
23
export default App;
24
and my navigation is this code as well
JavaScript
1
53
53
1
import React, { Component } from 'react';
2
import { NavLink } from 'react-router-dom';
3
import"./home.css";
4
5
const Home = () => {
6
return (
7
<React.Fragment>
8
<nav className="navbar navbar-expand-lg ">
9
<div className="container-fluid">
10
<i className="fas fa-paw fa-2x"></i>
11
<a className="navbar-brand " href="#" >Pets Forever</a>
12
<div className="collapse navbar-collapse justify-content-end" id="navbarNav">
13
<ul className="navbar-nav mr-auto">
14
<li className="nav-item active">
15
{console.log("shit")}
16
<a className="nav-link" href="/home">Home <span className="sr-only">
17
(current)</span></a>
18
{console.log("shit2")}
19
20
</li>
21
<li className="nav-item">
22
<a className="nav-link" href="#">Shop</a>
23
{console.log("shit3")}
24
25
</li>
26
<li className="nav-item">
27
<a className="nav-link" href="#">Contact Us</a>
28
{console.log("shit4")}
29
30
</li>
31
<li className="nav-item">
32
<a className="nav-link" href="#">Login</a>
33
{console.log("shit5")}
34
35
</li>
36
<li className="nav-item">
37
<a className="nav-link" href="#">Sign Up</a>
38
{console.log("shit6 ")}
39
40
</li>
41
42
</ul>
43
</div>
44
</div>
45
</nav>
46
47
</React.Fragment>
48
49
);
50
}
51
52
export default Home;
53
though i was working navlink and yet it didn’t work and showed some weird error but i need to work this route first please
Advertisement
Answer
Your implementation seems to be incomplete. You’re missing BrowserRouter
. Also, look at this response – it shows well how to use useRoutes
.
JavaScript
1
19
19
1
import React, { Component } from 'react';
2
import Home from './home';
3
import {BrowserRouter as Router, Route, Routes} from "react-router-dom"
4
5
class App extends React.Component {
6
render() {
7
return (
8
<React.Fragment>
9
<Router>
10
<Routes>
11
<Route path="/home" element={<Home />} />
12
</Routes>
13
</Router>
14
</React.Fragment>
15
);
16
}
17
}
18
export default App;
19