Home > database >  Detect the url of redirection in React Router v6
Detect the url of redirection in React Router v6

Time:08-06

I am using React v6, and I want to have a button that redirects the user to the page which redirected him to the current page.

And I know this question may hold two different questions, so here's what's exactly the question is focusing on:

  • the page which redirected to the current page is not from another domain.
  • the user got redirected to the current page by clicking a button somewhere else in the same react app I'm working on.

I couldn't find a clean way to do so using react v6, all the other answers are showing solutions using obsolete and extremely old versions of react-router-dom.

CodePudding user response:

Isn't it an option just to use something like that?

    const navigate = useNavigate();
    ...
    navigate(-1);

CodePudding user response:

i am not sure if you want to do something in the page from where you are redirecting. if you want to redirect instantly you can do the following

const navigate = useNavigate()
...
// when clicked on button
const handleClick = () => {
  navigate("redirect")
  navigate("current url", { replace: true })
}
  • Related