简体   繁体   中英

Detect the url of redirection in React Router v6

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 .

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

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

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 })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM