Ask Your Question
4

What is the method for obtaining the previous URL in Next.js?

asked 2023-06-11 11:04:55 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-11 11:07:02 +0000

huitzilopochtli gravatar image

In Next.js, you can obtain the previous URL using the useRouter hook from the next/router module. You can then access the previous URL using the asPath property of the router object. Here's an example:

import { useRouter } from 'next/router';

function MyComponent() {
  const router = useRouter();
  const { asPath } = router;

  // use `asPath` as needed

  return <div>...</div>;
}

The asPath property contains the previous URL as a string, including any query parameters or hash fragments. Note that this value is only available when using the client-side router, so it may be undefined on the initial request.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-11 11:04:55 +0000

Seen: 14 times

Last updated: Jun 11 '23