Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js Router.push() reloads page automatically

Is there a way to execute a Router.push() without reloading the page using Next.js?

I am using a [...all].js main file for a page that based on the url renders different components, but i want to be able to move between them without triggering a full page reload - it is a multi-step form.

In other words, I want to be able to move between the steps of the form by also updating the URL from 'form/1' to 'form/2'

like image 496
Francesco S Avatar asked Apr 09 '20 20:04

Francesco S


People also ask

Does router push reload the page?

router. push by default doesn't trigger a page reload · Issue #21787 · vercel/next.

How can I change URL without reloading page in NextJs?

Shallow routing allows you to change the URL without running data fetching methods again, that includes getServerSideProps , getStaticProps , and getInitialProps . You'll receive the updated pathname and the query via the router object (added by useRouter or withRouter ), without losing state.

Does react router refresh the page?

react-router-dom allows us to navigate through different pages on our app with/without refreshing the entire component. By default, BrowserRouter in react-router-dom will not refresh the entire page.


1 Answers

Set option shallow to true to avoid page refresh

router.push('/?step=2', null, { shallow: true })
like image 141
naijab.com Avatar answered Sep 29 '22 08:09

naijab.com