When I add routing with React Router to my React app, it throws error and shows warning:
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:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app
and
Uncaught TypeError: dispatcher is null
This is my Layout:
index.js:
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
App.js:
import Layout from "./components/Layout/Layout";
function App() {
return (
<div className="App">
<Layout />
</div>
);
}
export default App;
Layout.js:
import { Route, Routes, Outlet } from "react-router";
import Header from "./Header/Header";
function Layout(){
return(
<div id="layout">
<Header />
<Routes>
<Route path="/" element={<b>Home page</b>} />
<Route path="trade" element={<b>Trade page</b>} />
<Route path="/forum" element={<b>Forum page</b>} />
<Route path="/about" element={<b>About page</b>} />
</Routes>
</div>
);
}
export default Layout;
package.json:
{
"name": "theory_workaround",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}