Getting an error after using react router #69874
-
|
I'm new to react. I'm working on developing my react project. My react components are working fine. When i install react router and use it in app.js i got some error and my webpage got blank! But when i remove react router from app.js page my web page works fine. I don't understand why this happening. Error code are below. It will be helpful if anyone has the solution. Error code: react-dom.development.js:26874 Uncaught Error: useHref() may be used only in the context of a component. import './App.css'; function App() { <Route path='/' element={}> <Route path='/home' element={}> <Route path='/inventory' element={}> ); } export default App; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Issue function App() { // <-- (2) render router around app code <Route path='/' element={} /> <Route path='/home' element={} /> <Route path='/inventory' element={} /> ); } export default App; |
Beta Was this translation helpful? Give feedback.
Issue
Uncaught Error: useHref() may be used only in the context of a component.
This error is informing you that you are attempting to use some react-router-dom functionality outside of any routing context provided by a router. In other words, your app appears to be missing any router component to provide the routing context to the various Link, Routes, and Route components it is rendering.
Solution
Render the app code within a router.
Example:
import './App.css';
import Home from './components/pages/Home/Home/Home';
import Header from './components/shared/Header/Header';
import {
BrowserRouter as Router, // <-- (1) import router
Route,
Routes
} from 'react-router-dom';
import Inventory f…