What is the trade off between history push and replace?

ReactjsReact RouterBrowser History

Reactjs Problem Overview


I use History package to redirect invalid URL to 404 page on my react component.

I use .replace instead of .push simply because I don't want the browser to record any invalid url I have visited.

Have I missed the point here? And is there a trade-off between choosing one or the other for my purpose?

Can someone explain to me? thanks in advance ;)

Reactjs Solutions


Solution 1 - Reactjs

I think you have the right idea.

I personally use .replace when redirecting from an invalid url (or non relevant, i.e. redirect to "profile" page when a logged user goes to the "/login" url), so using the browser's "back" button works properly for the end user.

Solution 2 - Reactjs

router.replace acts like router.push, the only difference is that it navigates without pushing a new history entry, as its name suggests - it replaces the current entry.

Solution 3 - Reactjs

I like to use .replace() when i have a definitive action and i dont want the user to be able to come back to this page.

Example: i have a product detail page with a delete button to ultimately deletes the product. When i click the button i do router.replace back to the list of all products so when the user uses the back button in the browser he is not able to go back to the details page so he cannot see or even click again the delete button as this product does not exist anymore.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKris MPView Question on Stackoverflow
Solution 1 - ReactjsvgrafeView Answer on Stackoverflow
Solution 2 - ReactjskrankubaView Answer on Stackoverflow
Solution 3 - ReactjsRobert NiestrojView Answer on Stackoverflow