导航后在 NextJs 13(带有应用程序目录)中重新渲染(根-)布局?

Pet*_*rus 4 layout typescript reactjs next.js

我的根布局包含我想要在每个页面上显示的导航以及子页面/布局的子属性:

export default function RootLayout({children}: {children: React.ReactNode}) {
return (
  <html lang="en">
    <head />
    <body>
      <Navbar isLoggedIn={isLoggedIn} />
      <maina>
        {children}
      </main>
    </body>
  </html>
)
Run Code Online (Sandbox Code Playgroud)

导航包含我想有条件显示的链接,例如注册登录注销。注册或登录后,客户端通过 router.push('/') 重定向到主页并发送 JWT 令牌。

我希望isLoggedIn()再次执行根布局中的函数,但根据 NextJs 文档,布局不会重新渲染并保持状态。

如何通知布局状态(用户已登录/注销)发生了变化?

pat*_*wed 5

router.refresh()拨打电话后即可拨打router.push('/')。例如:

const handleSignUp = () => {
    router.push('/');
    router.refresh();
};
Run Code Online (Sandbox Code Playgroud)

这样,页面就会刷新。这是Next.js 文档的链接。