[Bug]: v6 BrowserRouter set basename, not match “/”
See original GitHub issueWhat version of React Router are you using?
6.0.2
Steps to Reproduce
step 1:
import { Route, Routes, BrowserRouter, Navigate } from 'react-router-dom';
step 2:
// ...
const Login = () => {
return (
<div>
login page
</div>
);
};
const Home = () => {
return (
<div>
Home page
</div>
);
};
// ...
<BrowserRouter basename='fe'> // basename set a word,example: fe
<Routes>
<Route path='/login' element={<Login />} />
<Route path='/' element={<Navigate to='/login' replace={true} />} />
<Route path='/home' element={<Home />} />
{/* <Route path='*' element={<Login />} /> */}
</Routes>
</BrowserRouter>
step 3:
Open the browser;
Enter the address URL:http://localhost:port is error, example step 4;(But v5 is ok)
But I must enter the full address URL: http://localhost:port/fe is ok;
step 4:
browser Console:<Router basename="/fe"> is not able to match the URL "/" because it does not start with the basename, so the <Router> won't render anything.
Expected Behavior
The user does not enter the complete URL containing the basename, I hope that the browser can enter the root domain name, there is also a way to match Login
Actual Behavior
browser Console:<Router basename="/fe"> is not able to match the URL "/" because it does not start with the basename, so the <Router> won't render anything.
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (1 by maintainers)
Top Results From Across the Web
Router basename is not able to match the URL because it ...
I need the URL fragments so that when a user refreshes a page or bookmarks a URL, it actually works. How to make...
Read more >Migrating to React Router v6: A complete guide
Migrate your React Router applications from v5 to v6 with this in-depth guide, including a review of additions and improvements from v5.
Read more >Upgrading from v5 v6.6.1 - React Router
Routes are chosen based on the best match instead of being traversed in order. This avoids bugs due to unreachable routes because they...
Read more >React Router v6 - Remix
However, one unintended side effect of this approach was that <Route path> would only ever match the beginning of a URL pathname since...
Read more >The Search Engine You Control
To fix the BrowserRouter import error, you need to fix a typo in your import. It's BrowserRouter with an "r", not BrowserRoute ....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is the expected behavior. You need to include the basename in the URL for the page itself for the Router to match anything.
I had to fix the missing redirect to basename during migration from v5 in client-only project and I’ve ended up with this solution:
index.jsxutils/ensure-basename.js