How do I perform a redirect after a POST request using the latest version (v5+) of react-router-dom in React?

In my projects, after switching to react-router-dom v5 or later, I moved away from conditionally rendering <Redirect>.

Instead, I grab the history object with useHistory() and then call history.replace(‘/target-route’) or history.push(‘/target-route’) after the POST request finishes.

This lets you control the browser history precisely and avoids rendering complications.

Using push adds a new entry, replace swaps the current one. That’s how I handle redirects after POST in react-router-dom v5.