I am looking for a way to dynamically update the :id paramter in my react application using react router dom. I have these codes:
import Blog from './pages/Blogpage/Blog'
import {
BrowserRouter as Router,
} from "react-router-dom";
<Route path="/allPosts/page/:id">
{<Blog/>}
</Route>
This is the link on navigation bar that takes user to the component called Blog.
<li className='topListItem'>
<Link className='link' to={`/allPosts/page/${sesssionPageNumber}`}>
BLOG
</Link>
</li>
Now, what I want to do is when a user clicks a button, the page address number changes. sessionPageNumber is a useState and I would like the URL which has :id parameter to also change as the useState number increases or decreases.
const handleNext = ()=>{
setPage(page + 1)
}
<MdNavigateNext onClick={handleNext} className='custom-next-prev-icon'/>
The problem is that the URL parameter does not change as the useState is updated via the button. The URL :id parameter remains the same which is the default value.
Is there a way I can update the URL :id parameter when the handleNext function is called on the button?