Home > OS >  React router: no matching routes when using descendent routes
React router: no matching routes when using descendent routes

Time:07-25

So I am making this project where I want to go to a path which has a breadcrumb component. And in that component I want to declare 3 paths which is accessible only in that page. kinda similar to this page : Publications | Stanford Ultrasound Imaging & Instrumentation Lab | Stanford Medicine .So for that I used react router v6 descendent routes. The breadcrumb component is showing but whenever I click any of the breadcrumb links, the child component is not rendering. It is giving me the error 'No matching routes found /pathnameundefinedundefined'Here is what I have done so far.(where the parent element has been declared)

import {
 Routes,
 Route,
 Link as RouterLink,
 useLocation,
} from "react-router-dom";


import { TransitionGroup, CSSTransition } from "react-transition-group";
import "./sidebar-animation.css";

export default function Sidebar() {
 
 let location = useLocation();


 return (
 <div>
 
 <Box width='100%' minHeight='100vh'>
 <TransitionGroup>
 <CSSTransition
 key={location.pathname}
 classNames='fade'
 timeout={300}
 >
 <Routes location={location.pathname}>
 <Route path='/' element={<AboutUs />} />
 <Route path='publications/*' element={<Publications />} /> //used the wildcard

 </Routes>
 </CSSTransition>
 </TransitionGroup>
 </Box>
 </Box>
 </div>
  );
}

and in the publications component

import { Text, Box } from "@chakra-ui/react";
import { Link, Routes, Route, useLocation } from "react-router-dom"; 
import { Breadcrumb, BreadcrumbItem, BreadcrumbLink } from "@chakra-ui/react"; 
import { TransitionGroup, CSSTransition } from "react-transition-group";
import FeatPublications from "./Featured Publications/featured-publications"; import JournalPublications from "./Journal Publications/journal-publications";

export default function Publications() { 
let location = useLocation(); 
return (
     <div id='publications'>

 <Box textAlign='center' mt={10}>  
 <Text fontSize='3xl' as='u' color='blue' fontFamily='Fjord One'>  
          Publications  
 </Text>  
 </Box>  
 <Box marginTop='6px' marginRight='6px'>  
 <Breadcrumb fontWeight='medium' fontSize='sm'>  
 <BreadcrumbItem isCurrentPage>  
 <BreadcrumbLink as={Link} to='/featured-publications'>  
              Journal Papers  
 </BreadcrumbLink>  
 </BreadcrumbItem>  


     <BreadcrumbItem>

 <BreadcrumbLink as={Link} to='/journal-publications'>  
              Conference Proceedings  
 </BreadcrumbLink>  
 </BreadcrumbItem>  


     <BreadcrumbItem>

 <BreadcrumbLink as={Link} to='/featured-publications'>  
              Book Contributions  
 </BreadcrumbLink>  
 </BreadcrumbItem>  
 </Breadcrumb>  
 <Box>  
 <Routes>  
 <Route  
 path='featured-publications'  
 element={<FeatPublications />}  
 />  
 <Route  
 path='journal-publications'  
 element={<JournalPublications />}  
 />  
 </Routes>  
 </Box>  
 </Box>  
 </div>  
  );  
}  

Am I using the link wrong? Need some help regarding this.

CodePudding user response:

The value of argument to in the BreadcrumbLink is wrong. As actual link will be the path followed by /publications path because the route under publications is descendants of path /publications.

<BreadcrumbLink as={Link} to='/publications/journal-publications'>  
              Conference Proceedings  
</BreadcrumbLink> 
<BreadcrumbLink as={Link} to='/publications/featured-publications'>  
              Book Contributions  
 </BreadcrumbLink> 
  • Related