I'm using React v.18 with React Router 6.3. When I am on a page and click a link to a front-end route, for example /groups/:groupId
, I will be temporarily redirected to a default "*"
route for a split second, before being directed to the desired route. Why is this happening and how can I fix this? Here is my App.js file:
function App() {
const location = useLocation();
const dispatch = useDispatch();
useEffect(() => {
dispatch(fetchKeywords());
}, []);
return (
<>
<NavMenu />
<Routes>
<Route path="/" element={<LoggedOutHome />} />
<Route path="/home" element={<HomeFeed />} />
<Route path="/login" element={<LogInForm />} />
<Route path="/signup" element={<SignUpForm />} />
<Route path="/searchresults" element={<SearchResults />} />
<Route path="/groups/:groupId" element={<GroupShow />} />
<Route path="/groups/:groupId/edit" element={<GroupEditPage />} />
<Route path="/groups/new"
element={
<GroupFormProvider>
<GroupFormIntro />
</GroupFormProvider>
}
/>
<Route path="/groups/:groupId/events/new" element={<EventNewForm />} />
<Route path="/events/:eventId/*" element={<EventHome />} />
<Route path="*" element={<NotFoundPage />} />
</Routes>
</>
);
}
export default App;
I've tried adding a slash in front of "*"
, and removing the useEffect
but the problem still persists.
Edit: To answer AKX and Drew's question, the flashing of the default route happens when a link coded as the below example is clicked:
<Link to={`/groups/${group.id}`} >Group Link</Link>
There is no explicit route protection. I am using create-react-app
. There is a backend in Rails, I don't think that is relevant.