If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
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.