Reuse React router routes constants with hooks when navigating
When use React router, sometimes we need to define the route path as constants like const USER = /user/:userId
, and the render the <Route path={USER} />
later. However, when we try to navigate, for example history.push()
, we have a problem which we need to replace that :userId
with real user id. And when we do it every time we calling history.push
, it won’t be any prettier. This is how I solved it with several lines of code.