difference between React router.push and router.replace?

siluveru kiran kumar picture siluveru kiran kumar · Oct 23, 2018 · Viewed 8.3k times · Source

What is the difference between React router.push and router.replace?

Answer

reisdev picture reisdev · Oct 23, 2018

The router history works like a stack of routes. When you use the router.replace, you're overwritting the top of the the stack. When using the router.push, it adds a new route to the top of the stack.

The router history allows you to go back to the last page. For example, when the user navigates to a invalid route, you can use the router.replace to prevent the user to navigate back to the invalid route.