So I have a NavBar component and the links created work fine if I am going from "/sites/1" to "/categories/1" or anything with "/sites" to "/categories" or even "/sites/1" to "/sites" but doesn't work when I am on a category show page aka "/categories/1" or "/categories/3" and trying to go to another category show page with the path "/categories/5" for example. I think the issue has to do with the fact that the exact path is dynamic and the router sees both paths as the same thing till there is a hard reload. Below is my code. Is there any way to use Link and get that component to re-render or do I have to use an anchor tag and reload each time?
import React, { useState, useEffect } from "react"
import { Switch, Route, Link, Redirect } from "react-router-dom"
import SitesIndex from "./SitesIndex";
import SiteShow from "./SiteShow";
import AddNewSiteForm from "./AddNewSiteForm"
import CategoryShow from "./CategoryShow"
const NavBar = props => {
const [categories, setCategories] = useState([])
const fetchCategories = async() =>{
try {
const response = await fetch("/api/v1/categories")
if(!response.ok){
const errorMessage = `${response.status} (${response.statusText})`
const error = new Error(errorMessage)
throw(error);
}
const categoryData = await response.json();
setCategories(categoryData.categories)
} catch (error) {
console.error(`Error in fetch: ${error.message}`)
}
}
useEffect(() =>{
fetchCategories()
},[])
const categoryLinks = categories.map(category =>{
return(
<li key={category.id} >
<Link to={`/categories/${category.id}`}>{category.name}</Link>
</li>
)
})
categoryLinks.unshift(
<li key={0}>
<Link to={`/sites`} >Home</Link>
</li>
)
return(
<div>
<div>
<ul className="nav-link">
{categoryLinks}
</ul>
</div>
<Switch>
<Route exact path="/" >
<Redirect to="/sites" />
</Route>
<Route exact path="/sites" component={SitesIndex} />
<Route exact path ="/sites/new" component={AddNewSiteForm} />
<Route exact path="/sites/:id" component={SiteShow} />
<Route exact path="/categories/:id" component={CategoryShow} />
</Switch>
</div>
)
}
export default NavBar