Reputation: 25
React router throwing this warning. even did what documentation says.
Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
in app.js i did this
import { BrowserRouter, Route, Routes } from "react-router-dom";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<div>Home page</div>} />
<Route path="/about" element={<div>aBout page</div>} />
</Routes>
</BrowserRouter>
);
}
export default App;
Upvotes: 1
Views: 1022
Reputation: 143
Maybe its about you are calling a div element instead of a React component.
Try converting to this:
<BrowserRouter>
<Routes>
<Route index element={<HomePage />}>
//index means it is the first page you are gonna encounter.
<Route path="/about" element={<AboutPage />} />
</Route>
</Routes>
</BrowserRouter>
And make sure you've created HomePage
and AboutPage
components and imported them.
Upvotes: 1