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

Alex picture Alex · Feb 16, 2017 · Viewed 103.5k times · Source

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.

Answer

Ayush Sharma picture Ayush Sharma · Feb 16, 2017

Try this,

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

warning works for versions prior to v4

and

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

for v4 and above