Client State Management with NextJS

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional NextJS Lessons

1.Course Intro
free
⏱️ 2:05
2.Get Setup
free
⏱️ 2:05
3.Project Template
free
⏱️ 5:45
4.Introducing FullStack React
free
⏱️ 4:57
5.App Router Reference
free
⏱️ 7:08
6.NextJS Server vs. "use client" Rendering
⏱️ 5:01
7.Nesting Server and Client Components
⏱️ 8:51
8.Client Only Components
⏱️ 3:48
9.Global CSS in NextJS
⏱️ 6:30
10.CSS Modules and SASS
⏱️ 4:15
11.Tailwind CSS
⏱️ 5:17
12.NextJS Custom Fonts
⏱️ 4:08
13.Static vs Dynamic Rendering
⏱️ 4:55
14.NextJS Link Component
⏱️ 3:20
15.NextJS Link Prefetching
⏱️ 5:49
16.Navigation Scroll with Link
⏱️ 2:02
17.Replace Navigation History
⏱️ 1:35
18.NextJS Image Component
⏱️ 7:01
19.Dynamic Image URLs
⏱️ 5:34
20.Safe Colocation with Private Folders
⏱️ 2:12
21.NextJS Route Groups
⏱️ 3:07
22.Dynamic Routes
⏱️ 5:37
23.NextJS API Routes
⏱️ 6:03
24.NextRequest and NextResponse
⏱️ 2:41
25.Cookies in API Handlers
⏱️ 4:25
26.React Suspense Simplified
⏱️ 2:23
27.Power of Suspense Boundaries
⏱️ 3:52
28.use Promises
⏱️ 3:24
29.Stream Promise Results from Server to Client
⏱️ 3:03
30.use Requires a Cached Promise
free
⏱️ 2:30
31.Suspense Error Boundaries
⏱️ 4:42
32.React Server Functions
⏱️ 4:50
33.Server Actions for Powerful Forms
⏱️ 5:35
34.useActionState to Simplify Form Actions
⏱️ 6:38
35.React Serializable Types
⏱️ 2:42
36.Create Dynamic Images with NextJS ImageResponse
⏱️ 5:54
37.Deploy NextJS Application to Production
⏱️ 3:51
38.NextJS Environment Variables Masterclass
⏱️ 3:50
39.Deploying and Managing Environment Variables
⏱️ 4:38
40.Using Databases for Dynamic Server Side State
free
⏱️ 7:34
41.Mastering NextJS Layout Files
⏱️ 4:54
42.Client State Management with NextJS
⏱️ 3:48
43.Client State Management In Action
⏱️ 8:04
44.Using Client State Management Libraries
⏱️ 5:25
45.Detecting Server vs Client Runtime and useEffect
⏱️ 4:33
46.Demystifying ISR vs SSR vs SSG
⏱️ 3:07
47.Static Site Generation or SSG with NextJS
⏱️ 3:16
48.Incremental Static Regeneration (ISR) Simplified
⏱️ 2:51
49.API Routes Caching with NextJS
⏱️ 3:05
50.Reference Guide to Caching for Modern NextJS
⏱️ 2:47
🚨 Work In Progress 🚧
Subscribe for Launch

Client State Management with NextJS

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

What is Client State Management?

With modern NextJS we essentially have two distinct applications running from the same project.

We have the server side application running on our server and then multiple instances of client side applications running in the users browsers.

We already know patterns of managing server state e.g. with databases which we covered in a previous lesson.

However there is a lot of dynamic state on the client that is only relevant when people are interacting with our application in the browser. So now in this lesson we will look at patterns of managing this dynamic client side state.

Using the Power of React

The objective of client side state management is that we want to give each client application their own store to manage any cross component shared state. This store has to be contained in a client component and Reacts built in client component for storing and sharing any global value among a set of components is context.

Basically if we render a context higher up in the render tree:

// Provide Context
export default function Parent() {
return (
<Context.Provider>
<Children />
</Context.Provider>
);
}

It automatically becomes available to any direct and indirect children:

// use Context
export function GreatGrandChild() {
const store = use(Context);

return <>...render → store.state ...on-events → store.actions()</>;
}

Context in Server Components

A question you might have at this point is how do you render the context in a server page and initialize the context store with some initial state from the server.

Well we know from our lessons on server vs client components, that we can render a client component on the server and even pass in server components as children:

export default async function ServerPage() {
return (
<>
<ClientComponent>
<ServerComponent />
</ClientComponent>
</>
);
}

The context provider is nothing more than a simple client component, so it can be:

  • Rendered on the server
  • Wrap other server and client components
  • Can even be passed some initial state from the server
export default async function ServerPage() {
const initialState = await loadData();
return (
<>
<Context.Provider initial={initialState}>
<ServerComponent />
</Context.Provider>
</>
);
}

And then we will be able to use this store in any child client component:

'use client';

// ServerPage -> ... -> SomeDeepChild
export function SomeDeepChild() {
const store = use(Context);

return <>...render → store.state ...on-events → store.actions()</>;
}

Context for Sections of your Site

Rendering the context in a the page component is fine if you want to manage global state for a particular route, but what about entire sections or your site or even the whole site. Fortunately the answer lies in layout components.

We can render the provider at the at the root layout and this makes it available for all routes (/**/*):

/** `/layout.tsx` */
export default async function RootLayout({ children }: React.PropsWithChildren) {
return (
<html lang="en">
<body>
<Context.Provider>{children}</Context.Provider>
</body>
</html>
);
}

Or render it in a layout specific to a sub-portion or our site e.g. /dashboard/**/*:

// `/dashboard/layout.tsx`
export default async function Layout({ children }) {
return <Context.Provider>{children}</Context.Provider>;
}
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

With modern x js, we essentially have two distinct applications running from the same code base. We might have multiple instances of our server application servicing various client browsers across the globe. So we essentially have multiple instances of our server side application running and multiple instances of our client side application running. This is why in a previous lesson we used an external database to manage dynamic global state on the server side. However, there is a lot of dynamic state on the client that is only relevant when people are interacting with our application in the browser.

00:33

So now in this lesson we will look at patterns of managing this dynamic client side state. The objective of client side state management is that we want to give each client application their own store to manage state specific to that user's interactions with their browser. The store has to be contained in the client component and reacts built in client component for storing and sharing any global state is something that we've already looked at in our react course, and it's called context. The idea is that we render a context provider that contains a store higher up in the tree of components. The value stored in the context will automatically become

01:07

available to any direct and indirect children of this component. So if some great grandchild of this component needs access to the context, all that it needs to do is to fetch the value with use. This will give it access to that global store and we can render out any current state to the user interface and even via client side events like clicks and keystrokes to store actions that can mutate the current state. A question you might have at this point is how do you render the context in a server page and initialize the context store with some initial state from the server? We know from our lessons on server versus client components

01:40

that we can render a server component within a server page and we can even render a client component within a server page. What's more is that when we are rendering within a server context, we can even pass a server component as a child to a client component. The context provider is nothing more than a client component, which means that we can render it on the server and pass in any additional server components that we want. And since we are currently within a server context, we can make any server only API calls to load some initial data that we can then pass on to the context provider. Once we have this set up within our server page,

02:13

any client only components within this page will be able to access the context with the use API. This will give them access to that global store, which they can then use to render any stage to the user interface or via any events to store actions. Rendering the context in a page component is fine if you want to manage global state for a particular route, but what about entire sections of your site or even the whole site? Fortunately the answer lies in layout components. For example, within the root layout of our application, we could render a context provider that wraps anything that our application is going to display.

02:46

This makes the store contained within the context available for all routes within our application. Any client component anywhere within our application will be able to get access to whatever is contained within this context. Doesn't matter if It's rendered within slash admin or slash dashboard or slash admin dashboard, all routes will have access to this context. But what if you want a global store only for a specific portion of our website, for example, only within slash dashboard? Well, instead of using the context within the global layout, we can create a new layout for a specific subro. For example, we can create a layout under the dashboard

03:20

folder and use the context within that. This context is going to be specific only for routes that exist within slash dashboard. So now on any component on our site that is rendered within a route that starts with slash dashboard, we will be able to use this particular context. Doesn't matter if we are on slash dashboard security or dashboard subscription or dashboard subscription security, as long as we are on a page that is under slash dashboard, we can use the state management store provided by this context.

Professional NextJS

Professional NextJS

1.Course Intro
free
⏱️ 2:05
2.Get Setup
free
⏱️ 2:05
3.Project Template
free
⏱️ 5:45
4.Introducing FullStack React
free
⏱️ 4:57
5.App Router Reference
free
⏱️ 7:08
6.NextJS Server vs. "use client" Rendering
⏱️ 5:01
7.Nesting Server and Client Components
⏱️ 8:51
8.Client Only Components
⏱️ 3:48
9.Global CSS in NextJS
⏱️ 6:30
10.CSS Modules and SASS
⏱️ 4:15
11.Tailwind CSS
⏱️ 5:17
12.NextJS Custom Fonts
⏱️ 4:08
13.Static vs Dynamic Rendering
⏱️ 4:55
14.NextJS Link Component
⏱️ 3:20
15.NextJS Link Prefetching
⏱️ 5:49
16.Navigation Scroll with Link
⏱️ 2:02
17.Replace Navigation History
⏱️ 1:35
18.NextJS Image Component
⏱️ 7:01
19.Dynamic Image URLs
⏱️ 5:34
20.Safe Colocation with Private Folders
⏱️ 2:12
21.NextJS Route Groups
⏱️ 3:07
22.Dynamic Routes
⏱️ 5:37
23.NextJS API Routes
⏱️ 6:03
24.NextRequest and NextResponse
⏱️ 2:41
25.Cookies in API Handlers
⏱️ 4:25
26.React Suspense Simplified
⏱️ 2:23
27.Power of Suspense Boundaries
⏱️ 3:52
28.use Promises
⏱️ 3:24
29.Stream Promise Results from Server to Client
⏱️ 3:03
30.use Requires a Cached Promise
free
⏱️ 2:30
31.Suspense Error Boundaries
⏱️ 4:42
32.React Server Functions
⏱️ 4:50
33.Server Actions for Powerful Forms
⏱️ 5:35
34.useActionState to Simplify Form Actions
⏱️ 6:38
35.React Serializable Types
⏱️ 2:42
36.Create Dynamic Images with NextJS ImageResponse
⏱️ 5:54
37.Deploy NextJS Application to Production
⏱️ 3:51
38.NextJS Environment Variables Masterclass
⏱️ 3:50
39.Deploying and Managing Environment Variables
⏱️ 4:38
40.Using Databases for Dynamic Server Side State
free
⏱️ 7:34
41.Mastering NextJS Layout Files
⏱️ 4:54
42.Client State Management with NextJS
⏱️ 3:48
43.Client State Management In Action
⏱️ 8:04
44.Using Client State Management Libraries
⏱️ 5:25
45.Detecting Server vs Client Runtime and useEffect
⏱️ 4:33
46.Demystifying ISR vs SSR vs SSG
⏱️ 3:07
47.Static Site Generation or SSG with NextJS
⏱️ 3:16
48.Incremental Static Regeneration (ISR) Simplified
⏱️ 2:51
49.API Routes Caching with NextJS
⏱️ 3:05
50.Reference Guide to Caching for Modern NextJS
⏱️ 2:47
🚨 Work In Progress 🚧
Subscribe for Launch