I'm using react-router v6 and I'm trying to use a custom history to be able to navigate in redux actions (outside of a component). So I'm using Router from 'react-router
instead of BrowserRouter from 'react-router-dom
as mentionned in the doc.
Here is my code:
index.js
import myHistory from './utils/history';
import { Router } from 'react-router';
ReactDOM.render(
<Provider store={store}>
<Router history={myHistory}>
<App />
</Router>
</Provider>,
document.getElementById('root')
);
App.js
import React from 'react';
import MainNavigation from './components/navigations/MainNavigation';
import Routes from './routes/Routes';
const App = () => {
return (
<>
<MainNavigation />
<Routes />
</>
);
};
export default App;
MainNavigation.js
import React from 'react';
import { Link } from 'react-router-dom';
const MainNavigation = () => {
return (
<>
<nav className='navigation'>
<ul>
<li>
<Link to='/'>Home</Link>
</li>
<li>
<Link to='/contact'>Contact</Link>
</li>
<li>
<Link to='/about'>A propos</Link>
</li>
<li>
<Link to='/user-profile'>Votre compte</Link>
</li>
</ul>
</nav>
</>
);
};
export default MainNavigation;
Routes.js
//** Import routers */
import {Route, Routes as Routing } from 'react-router-dom';
const Routes = () => {
return (
<Routing>
<Route path='/' element={<Home />} />
<Route path='/about' element={<About />} />
<Route path='/contact' element={<ContactForm />} />
<Route path='/user-profile' element={<UserAccount />} />
</Routing>
);
};
export default Routes;
I really can't figure out why I got the error message Cannot read properties of undefined (reading 'pathname'). It's working when I swap to BrowserRouter instead of Router but then I can't use history ('navigate' now in v6) in my app.
Thank you.