Ask Your Question
1

How can I make Next.js navigate to the same page when the user clicks on Continue?

asked 2022-04-19 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-08-30 22:00:00 +0000

nofretete gravatar image

You can use the router.push method provided by Next.js to navigate to the same page when the user clicks on Continue. Here is an example:

import { useRouter } from "next/router";

function MyForm() {
  const router = useRouter();

  function handleSubmit(event) {
    event.preventDefault();
    // Process the form data here...

    // Navigate to the same page
    router.push(router.pathname);
  }

  return (
    <form onSubmit={handleSubmit}>
      {/* Form fields here */}
      <button type="submit">Continue</button>
    </form>
  );
}

In this example, we are importing the useRouter hook from Next.js and initializing it in the component. When the user submits the form, we prevent the default behavior (page reload) and process the form data. Then we call router.push with the current pathname to navigate to the same page without reloading it.

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: 2022-04-19 11:00:00 +0000

Seen: 19 times

Last updated: Aug 30 '21