Nextauth_const { data:} = getString();在服务器端渲染在nextjs

uemypmqf  于 2023-04-20  发布在  其他
关注(0)|答案(1)|浏览(69)

这是我在page.tsx上的代码(服务器端渲染)

<NavigationBar session={session} />

并将其传递到“使用客户端”页面

const NavigationBar = ({ session }: any) => {}

如何避免这个错误我仍然希望我的page.tsx作为服务器端呈现

app/page.tsx (17:38) @ useSession

  15 | export default function Home() {
  16 | 
> 17 | const { data: session } = useSession();
     |                                    ^
  18 | 
  19 | 
  20 | return (
v2g6jxz6

v2g6jxz61#

useSession是react hook。hook只能在客户端组件中使用。hook是一个挂接到react生命周期中的函数。如果你想使用服务器组件,你必须使用

import { getServerSession } from "next-auth";

const ServerComponent = async () => {
  const session = await getServerSession(authOptions);
  return ()
}

我在这里解释了auth选项:如何在应用程序目录下的下一个js13测试版服务器组件中使用下一个auth getServerSession

相关问题