And it's going to bleed data. So that's not great. That's why you don't want to use globals.
So to put this in a more practical context, let's do a donuts and dragoons store. And this is gonna be in the app writer so you'll have a layout at the top that holds the whole page. You'll have a header and then a cart pop-up. You'll have the content of the page, so this is like an e-commerce kind of page.
And the ones that we're really interested in are these two components, the cart pop-up and the add to cart pop-up. Because those are client components, they're gonna mutate state, it's going to be mutable. And that's when we want to use something, maybe, like a state manager or not. Let's just use context. If you look at this and you're like, oh, that's so simplistic, all you have is a number, yeah, that's true. You know, I mean, you probably don't need to use a state manager for this, but let's just say you do.
So the old school thinking would be like, okay, so we're gonna have our global store, like I talked about before, and we'll just get a redux provider, point it at the global store, and there you go. So again, it would look like that. You'd have a provider, give it a store that you just created, and then when you want to use it, you give it a use selector or use dispatch and then you can talk to that store. New school thinking, so how do we fix that? How do we fix having that store in global space like that? Well, what you do is you can just create a new component, a new client component, we'll call it store provider, and just move that store into there. Now, for every single request, we have that store uniquely within that tree of components and avoid any kind of potential for cross request pollution.
So where we used to have something like this, now we're going to go have a function called create store that does exactly the same thing, but gives you back a store dynamically. So you can create a new one for each request in that client component, so let's go take a look at what that is. So we have a store provider, pretty simple, we're going to use a use ref to store the output of that create store, and then we'll just use the simple old Redux provider to provide that down to our components. If you have some sort of initial state, you can start off with that use ref being null, go to check to see if it's null, create the store if it's null and then initialize the store by using a dispatch. And the nice thing about that is that if you have state in here in the store provider, disassociate states, it's also doing other things, I don't know, something else like showing the menu or whatever, then you can run this a bunch of times and it'll only ever do this on initial startup. To use it, we just use use selector, just like we did before. Same thing. So from there on down, it's like just using Redux. Here it comes, though, when you get per route state. Because, something specific about this. So let's say we have our, this is our store, right? We've got two new components in there. We also want to drive the deep, and the reviews.
Comments