Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the URL in react-router v4 without using Redirect or Link [duplicate]

I'm using react-router v4 and material-ui in my React app. I was wondering how to change the URL once there is a click on a GridTile within GridList.

My initial idea was to use a handler for onTouchTap. However, the only way I can see to redirect is by using the components Redirect or Link. How could I change the URL without rendering those two components?

I've tried this.context.router.push('/foo') but it doesn't seem to work.

like image 502
Alex Avatar asked Feb 16 '17 10:02

Alex


People also ask

What can I use instead of redirect in React router Dom?

Redirect and Navigate Component With the release of React Router v6, the Redirect component was removed and replaced with the Navigate component, which operates just as the Redirect component does by taking in the to prop to enable you redirect to the page you specify.

Does React router change URL?

Using react-router-dom to Change URL Path and Render Components. The react-router-dom package is great for rendering different React components based on the url path. Therefore, React components can lead to others by changing the url path.

How do I change the URL in React app?

You may just need to edit the homepage entry. Alternatively, if you plan on deploying the app into a nested directory on your server then you can specify a basename prop on the router. can you add your router code here? Change homepage to "./" and try it again locally.


1 Answers

Try this,

this.props.router.push('/foo') 

warning works for versions prior to v4

and

this.props.history.push('/foo') 

for v4 and above

like image 154
Ayush Sharma Avatar answered Oct 22 '22 13:10

Ayush Sharma