简体   繁体   English

使用 nextjs 中其他页面的参数集初始化组件

[英]Initializing component with parameter set from other page in nextjs

I have 2 components on separate pages in nextjs.我在 nextjs 的不同页面上有 2 个组件。 The way I would like my app to work is:我希望我的应用程序工作的方式是:

  1. User inputs some data on page A and clicks submit用户在页面A输入一些数据并点击提交
  2. A request to my backend is sent and it returns a 'accessToken', at the same time the user is redirected to page B向我的后端发送了一个请求,它返回一个“accessToken”,同时用户被重定向到页面 B
  3. To load page B the 'accessToken' is sent to an external service to initialize the component provided by that service为了加载页面 B,“accessToken”被发送到外部服务以初始化该服务提供的组件
  4. If the user leaves the page and returns, the 'accessToken' should be still set and they should not need to re-do step 1,2 but if they request a new one then that should also be updated in page B如果用户离开页面并返回,'accessToken' 应该仍然设置,他们不需要重新执行步骤 1,2,但如果他们请求一个新的,那么也应该在页面 B 中更新

Below the component provided by the external service for reference:下面由外部服务提供的组件供参考:

<WebSdk
  accessToken={token}
  expirationHandler={handler}
  config={config}
  options={options}
  onMessage={messageHandler}
  onError={errorHandler}
/>

How should I store and read the access token?我应该如何存储和读取访问令牌? Do I need to use useState or useEffect ?我需要使用useState还是useEffect吗? Do I need to set a cookie or localStorage?我需要设置 cookie 或 localStorage 吗?

Neither useState nor useEffect is good choice for this condition.对于这种情况, useStateuseEffect都不是好选择。

You can use both cookies(low security) and localStorage , but I recommend using sessionStorage(it has expire time) .I您可以同时使用cookies(low security)localStorage ,但我建议使用sessionStorage(it has expire time) 。我

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

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