typescript - Next.js 不会在 _app.tsx 中传递用户属性
问题描述
我想将用户对象传递给我的 next.js 应用程序中的所有页面。我没有将任何内容写入控制台或使用以下代码作为道具传递。放大部分似乎正在工作。
// _app.tsx
import type { AppProps } from "next/app"
import type { GetServerSideProps, GetServerSidePropsContext } from "next"
import Amplify, { Auth, withSSRContext } from "aws-amplify"
import { AmplifyAuthenticator, AmplifySignIn } from "@aws-amplify/ui-react"
import { config } from "../amplify.config"
Amplify.configure({ ...config, ssr: true })
MyApp.getServerSideProps = async (context: GetServerSidePropsContext) => {
const appProps = await getServerSideProps(context)
return { ...appProps }
}
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const auth = withSSRContext(ctx).Auth as typeof Auth
let user: any
try {
user = await auth.currentAuthenticatedUser()
console.log("user is authenticated:", user)
return { props: { user: user } }
} catch (err) {
console.log("error: no authenticated user")
}
}
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<AmplifyAuthenticator>
<AmplifySignIn hideSignUp usernameAlias="email" slot="sign-in" />
<Component {...pageProps} />
</AmplifyAuthenticator>
)
}
解决方案
请阅读文档的这一部分next.js
https://nextjs.org/docs/advanced-features/custom-app
应用目前不支持 Next.js 数据获取方法,如 getStaticProps 或 getServerSideProps。
因此,您必须对自己进行一些更改_app.js
才能实现这一目标。
您可以在这篇文章中找到解决方案: Next.js:减少数据获取并在页面之间共享数据
推荐阅读
- nsepy - 如何从 get_history 函数中获取相关数据,只说“基础价格”,没有日期
- python - 比较python中的文件和文件夹名称
- shell - 如何在linux shell脚本中获取hbase的所有表并对每张表进行操作?
- html - 在导航栏 bootstrap-4 Beta 下方放置一个背景图片
- php - Redmine + PHP Wamp:如何在现有 Wamp 服务器中安装 Redmine
- ios - 无法仅重新加载选定的单元格
- java - 异步 Web 服务方法 (JAVA CXF)
- sql - SQL - 以两种不同方式查询相关表
- hadoop - 嵌套选择在蜂巢中不起作用
- r - 读取多个 xlsx 文件,每个文件都有多个工作表 - purrr