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 文档,布局不会重新渲染并保持状态。
如何通知布局状态(用户已登录/注销)发生了变化?
router.refresh()拨打电话后即可拨打router.push('/')。例如:
const handleSignUp = () => {
router.push('/');
router.refresh();
};
Run Code Online (Sandbox Code Playgroud)
这样,页面就会刷新。这是Next.js 文档的链接。
| 归档时间: |
|
| 查看次数: |
2132 次 |
| 最近记录: |