简体   繁体   English

使用 Next.js 更改路线而不重新加载页面

[英]Change route without reloading page with Next.js

Whenever the user clicks on the continue button, a function is fired and in that function, I call:每当用户单击继续按钮时,都会触发 function,在该 function 中,我调用:

push("/signup/page-2", undefined, { shallow: true });

All dynamic routes that looks like /signup/[page].js that returns <Component />所有看起来像/signup/[page].js的动态路由返回<Component />

I also have a /signup/index.js that returns <Component />我还有一个返回<Component />/signup/index.js

The Problem is:问题是:

The page reloads and all state is cleared each time push("/signup/page-2", undefined, { shallow: true });每次push("/signup/page-2", undefined, { shallow: true });页面都会重新加载并清除所有 state is called.叫做。

It's also worth mentioning that this app was a CRA with react-router-dom app that was converted to a next.js app还值得一提的是,这个应用程序是一个带有 react-router-dom 应用程序的 CRA,它被转换为一个 next.js 应用程序

You got it all wrong about shallow-routing .你对shallow-routing完全错误。 Shallow-routing works for the same page URL and generally, you will update the query string parameters for the same page and the updated value of query params will be available through the router .浅路由适用于同一页面 URL并且通常您将更新同一页面的查询字符串参数,并且查询参数的更新值将通过router获得。

router.push('/signup/[page]', '/signup/page-1') will push the new URL path and will navigate to a complete different page from /signup/ . router.push('/signup/[page]', '/signup/page-1')将推送新的 URL 路径,并将导航到与/signup/完全不同的页面。 That is why shallow-routing doesn't work here and the state value resets.这就是为什么浅路由在这里不起作用并且state值重置的原因。

In the pages directory /signup/[page].js and /signup/index.js are two completely different files in the file system. pages目录中的/signup/[page].js/signup/index.js是文件系统中两个完全不同的文件。 If you try to navigate between them with shallow-routing it will not work.如果您尝试使用浅路由在它们之间导航,它将无法正常工作。

Solution解决方案

You don't need another dynamic route like /signup/[page] here, you can update the page param through query string and keep the same URL path.这里不需要像/signup/[page]这样的动态路由,可以通过查询字符串更新page参数并保持相同的 URL 路径。 In /signup/index.js page you can get the updated value of the query param and work on it./signup/index.js页面中,您可以获取查询参数的更新值并对其进行处理。

router.push({
   pathname: '/signup',
   query: {
      pageId: "page-1"  // update the query param
   }
}, undefined, { shallow: true})


// you have access to the updated value of pageId through router.query.pageId

const currPage = router.query.pageId

Ignore next router for this task, just use JavaScript to change the URL instead - components will not reload or be affected.忽略此任务的下一个路由器,只需使用 JavaScript 来更改 URL - 组件不会重新加载或受到影响。

window.history.pushState(null, 'Page Title', '/newUrl'); (null is stateData). (null 是 stateData)。

For example:例如:
You are on a users profile page localhost.com/john and you click on their post with the id 13983 :您在用户个人资料页面localhost.com/john上,然后单击他们的 ID 为13983的帖子:

function onPostClicked(post){
  showPost(post);
  window.history.pushState(null, 'Post by John', '/john/p/13983');
}

Then when they click away from the post you can return back to the original URL:然后当他们点击离开帖子时,您可以返回到原来的 URL:

function onPostClosed(){
  hidePost();
  window.history.pushState(null, 'John', '/john');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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