I have a Layout component which I'd like to use as a wrapper for my pages.
pages/_app.js
import { UIProvider, uiState } from '../components/Context/UIContext'
import { UserProvider, userState } from '../components/Context/UserContext';
import { useUser } from '../lib/hooks'
import Layout from '../components/Layout/index';
function MyApp({ Component, pageProps }) {
const { user, mutate, loading } = useUser();
return (
<UserProvider value={userState}>
<UIProvider value={uiState}>
<CloudinaryContext cloudName="hillfinders">
<Head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
</Head>
<Layout user={user} mutate={mutate} >
<NextNprogress
color="#abf581"
startPosition={0.3}
stopDelayMs={200}
height={4}
showOnShallow
/>
<Component {...pageProps} />
</Layout>
</CloudinaryContext>
</UIProvider>
</UserProvider>
)
}
export default MyApp
Essentially I am calling the swr useUser()
hook and passing the values from it to the Layout
component to show/hide elements of the nav of an login/unauthenticated user.
The problem is I noticed a lag in the nav elements appearing in the DOM after one logs in vs. when I import the Layout
component as a wrapper to each page and calling the hook in the Layout
declaration itself.
So for pages/login, pages/profile, pages/dashboard
etc. I am doing this:
import dynamic from "next/dynamic";
const Layout = dynamic(() => import('../components/Layout'));
export default function Dashboard() {
return (
<Layout>
<h1>Dashboard</h1>
</Layout>
)
}
And this is the Layout
component, where I call the useUser()
hook:
export default function Layout({ children }) {
const { user, mutate } = useUser();
<>
<div className="shadow bg-base-200 drawer h-screen">
<div className="flex-none hidden lg:block">
<ul className="menu horizontal">
<li>
<Link href="/">
<a>Home</a>
</Link>
</li>
{user && user.isVerified
?
<>
<li>
<Link href="/profile">
<a>Profile</a>
</Link>
</li>
<li>
<Link href="/dashboard">
<a>Dashboard</a>
</Link>
</li>
<li>
<a
role="button"
onClick={() => uidispatch({ type: 'showModal' })}
>
Logout
</a>
</li>
<li className="avatar">
<div className="rounded-full w-10 h-10 m-1">
<img src="https://i.pravatar.cc/500?img=32" />
</div>
</li></>
:
<>
<li>
<Link href="/login">
<a>Login</a>
</Link>
</li>
<li>
<Link href="/registration">
<a>Register</a>
</Link>
</li>
</>
}
</ul>
</div>
</div>
</>;
}
Is there a strategy I can use in the swr
to make it faster?