简体   繁体   English

NextJS:如何将数据从一页发送到另一页的getInitialProps函数

[英]NextJS: How to send data from one page to another page's getInitialProps function

I'm trying to send data from Page B (login) to Page A's (index) getInitialProps as I need a key for Page A's getInitProps to actually get the props... 我正在尝试将数据从页面B(登录)发送到页面A的(索引) getInitialProps因为我需要页面A的getInitProps的密钥才能实际获取道具...

I'm currently trying to do it by sending a POST request to a custom express route and use that route to render the Page server side so getInitialProps will run. 我目前正在尝试通过向自定义快速路由发送POST请求并使用该路由来呈现Page服务器端来执行getInitialProps。 But the problem is that the browser won't render the page even though it gets built server side. 但是问题是浏览器即使在服务器端构建页面也不会呈现该页面。

POST Request From Login Page (happens Client Side): 来自登录页面的POST请求(发生在客户端):

const headers = {'Content-type': 'application/json; charset=UTF-8'}
await fetch(`${getRootUrl()}/loadKey`, {method: "POST", body: JSON.stringify({key}), headers})

Express Route: 快速路线:

server.post('/loadKey', (req, res) => {
    // const {key} = req.body
    // console.log(key)
    next({dev}).render('/') //index page
})

The key gets logged fine and in the terminal, nextjs is saying that the index page is getting built,and getInitialProps gets ran, but nothing happens on the browser. 密钥已正确记录,在终端中,nextjs表示正在构建索引页面,并且运行了getInitialProps,但浏览器上没有任何反应。 No redirect. 没有重定向。

I've tried using express.redirect , express.render , and I've tried to force a client-side redirect, but for the former two, the same thing happens when I use next.render; 我尝试使用express.redirectexpress.render ,并且尝试强制客户端重定向,但是对于前两个,当我使用next.render时,会发生相同的事情。 the page gets built, getInitialProps runs, but no redirect happens on the browser. 页面被构建,getInitialProps运行,但是浏览器上没有重定向发生。 And when I force a redirect using Link or Router, then Index's getInitialProps won't get the key passed in from the POST request. 当我使用Link或Router强制进行重定向时,Index的getInitialProps将不会获得从POST请求传递的密钥。

Try this one 试试这个

server.post('/loadKey', (req, res) => {
    const {key} = req.body
    // Do whatever you need to do
    const actualPage = '/'
    const queryParams = { key }
    next({dev}).render(req, res, actualPage, queryParams)
})

Now you should have access to key in index page by props. 现在,您应该可以通过道具访问索引页面中的key

Your approach will only work for server-side rendered pages (since you need to use req ), not for client-side. 您的方法仅适用于服务器端呈现的页面(因为您需要使用req ),而不适用于客户端。 Even if you get it work once, your logic might break when you try navigating to this route, or you might end up with part of your app being SPA and then some refreshes when visiting this page. 即使您一次使用它,尝试导航到该路线时,逻辑也会中断,或者您最终可能会将应用程序的一部分变成SPA,然后在访问此页面时会有所刷新。

From nextjs documentation: 从nextjs文档中:

For the initial page load, getInitialProps will execute on the server only. 对于初始页面加载,getInitialProps仅在服务器上执行。 getInitialProps will only be executed on the client when navigating to a different route via the Link component or using the routing APIs. 仅当通过Link组件或使用路由API导航到其他路由时,才会在客户端上执行getInitialProps。

Some possible solutions that I've used in the past: 我过去使用过的一些可能的解决方案:

  • Implement your page B as a component, and call it from page A if certain criteria it's met. 将页面B实施为组件,如果满足某些条件,则从页面A调用它。 Page B won't have getInitialProps but page A does, and it seems like you need to know something from page A getInitialProps in order to correctly render page B, so why not use a single page instead? 页面B没有getInitialProps,但是页面A有,并且您似乎需要了解页面A的getInitialProps才能正确呈现页面B,所以为什么不使用单个页面呢? The user won't know the difference. 用户不会知道区别。

  • Use Redux or the likes. 使用Redux等。 You can store the value of your prop in the store, which it's available globally, even to getInitialProps. 您可以将道具的值存储在商店中,该道具在全球都可以使用,甚至可以存储到getInitialProps。 But beware! 但是要当心! Save your prop to the store before trying to access it on page B. A page's getInitialProps runs before the HoC's. 尝试访问第B页上的内容之前,请将其保存到商店。页面的getInitialProps在HoC之前运行。

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

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