How to navigate to another page without reloading the whole page in NextJs?

拥有回忆 提交于 2019-12-11 17:35:19

问题


I notice that in NextJs, whenever I click on a <Link> to go to another page, it calls the getInitialProps even if that "another" page is the same page.

For instance, on /profile page, there are 2 components, one for "user information" and the other component for "list of orders" which is paginated. Both data used in "user information" and "list of orders" components are fetched from APIs in getInitialProps of the /profile page.

When I click to go to the next page of the "list of orders" component on /profile page, it goes to /profile/p/2. The data loads and it all works fine, except that /profile/p/2 actually reloads the whole profile page and re-fetches the "user information" data which should have remained the same (and doesn't need to be re-fetched) as long as the page is part of /profile page.

Is there a way to navigate to /profile/p/2 from /profile without reloading the whole page but only the component showing the list of orders?


回答1:


You can pass shallow to Link like this

<Link href="...." as="...." shallow=true />


来源:https://stackoverflow.com/questions/55796012/how-to-navigate-to-another-page-without-reloading-the-whole-page-in-nextjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!