I am getting the following error when using react-router-dom version 6:
Uncaught Error: 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:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
It is saying my BrowserRouter is the problem which seems to point to a mismatch of React and the renderer. However, I can't find any issues. Below is the piece of code that is causing the error:
import React from 'react';
import './App.css';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import Home from "./pages/Home";
function App() {
return (
<div>
<BrowserRouter>
<Routes>
<Route path="/" element={Home}/>
</Routes>
</BrowserRouter>
</div>
);
}
export default App;
Doing a npm ls react yields the below:
├─┬ react-bootstrap@2.0.4
│ ├─┬ @restart/hooks@0.4.5
│ │ └── react@17.0.2 deduped
│ ├─┬ @restart/ui@0.2.5
│ │ ├─┬ @react-aria/ssr@3.1.0
│ │ │ └── react@17.0.2 deduped
│ │ └── react@17.0.2 deduped
│ ├─┬ prop-types-extra@1.1.1
│ │ └── react@17.0.2 deduped
│ ├─┬ react-dom@17.0.2
│ │ └── react@17.0.2 deduped
│ ├─┬ react-transition-group@4.4.2
│ │ └── react@17.0.2 deduped
│ ├── react@17.0.2
│ └─┬ uncontrollable@7.2.1
│ └── react@17.0.2 deduped
├─┬ react-router-dom@6.2.1 extraneous
│ └── react@17.0.2 deduped
└─┬ react-router@6.2.1 extraneous
└── react@17.0.2 deduped
I'm blocked by this and any help would be greatly appreciated! Thanks!
I found the issue! I have two package.json files - one in my react app and one outside of it. I had my react-router-dom installed in the wrong package.json. Installing it in the react-app itself fixed the problem.