React router push to history and preserve relative path

Node.JS

I am using react router and I have the following code:

let history = useHistory();

let goToReviewPage = () => history.push(`review/${productId}`);

My current url is: /foo/bar and calling goToReviewPage() will redirect me to

/foo/review/${productId} instead of /foo/bar/review/${productId}

I am not sure how to set the base path will pushing the history.

Daniel

One way is to use window.location to obtain the current path.

For example:

history.push(window.location.pathname + '/' + `review/${productId}`);

window.location api

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React router history.push is not adding path from root?

React Router with relative path deployment

Setting Relative Path in React Router

React Router BrowserRouter History Push & Google Analytics

Onsubmit and history push is not working react router

Vue Router "push" how to preserve path's query string

React router history.push() not working from history.listen()

React router `this.props.history.push` - history not defined

React router, codes after history.push run?

How to push to History in React Router v4?

react router is only replacing the final route on history.push

React router this.props.history.push , pushing the location but not rendering the component

React router history.push falls back to 404 route

React router history push always route to 404 page

react router doesn't re-render after history push

history.push using react-router-dom

Redux & React Router: Combing dispatch and navigation (history.push)

react-router Redirect vs history.push

React router not working as expected when pass state to history.push

How to push to History in React Router v5?

react-router-dom - Difference between link and history.push?

History.push a link to a new tab with react router

history.push() and Custom URL parameters using react-router

Problems that need to be refreshed when using history.push() in react router

React router not rendering the component through history.push()

Is possible to override History.push method of React-Router-Dom

react router relative path to static files not loading correctly

React router v4 nested route relative path

React router - undefined history