Current section: State Initializer 30 exercises
solution

Initialize Toggle

Loading solution

Transcript

00:00 So let's start out with making the initialization work. We'll do the reset here in a sec. So initial on, we'll set that to true. Of course, TypeScript's not happy with that. We don't have the types for it. So let's go into here, and we'll let our AI assistant fill this in for us.

00:17 So it defaults to an empty object if no object is provided. Our object type, it has an initial on property that's optional and it's a Boolean. If it's not provided, we default to false. And then we'll pass our initial on right there. And this should get us initializing to on. Perfect.

00:35 So then the reset functionality, if we come back over here, let's get rid of that. We'll pull reset from use toggle. We'll dive into here and pass that reset right there. And then we'll add the reset function here. So function, reset, and it dispatches. The type of dispatches is reset,

00:55 and we're gonna pass the initial state so we know what state to set it to. Let's come up here to our action types right there. And I'm just gonna let Marty the money bag fill that in for us because honestly, this isn't, we've already learned about this stuff. So we've got our type reset and our initial state. That's our toggle state that we want to reset it to.

01:14 And then we have our case for reset, which will return that initial state that the action gave to us. And that should reset us. Dun, dun, dun, dun. That works great. Now, before I wrap it up, hopefully that was pretty straightforward, but I want to just call out what makes this special

01:32 over the alternative mechanism for resetting a component, which is to change its key. So I'm gonna come over here to our app. I'm gonna call this our app info, and then we're gonna export a function called app. This way we can reset the app info.

01:51 So we're gonna return app info, and I'm gonna have some state initial. This'll be our, yeah, whoops, our key. We'll initialize it to a symbol, and we'll pull in use state from React. And then here, actually, let's add an on reset right here.

02:14 We'll pass on reset. So this is just another way to do reset, but we're gonna see what is the difference, not only in the way that this looks, but also in its actual functionality and why the reset functionality matters so much. So then we'll add our key. Whoops. Key is key. There we go.

02:34 This is blowing up because the key, it's not assignable. Symbol's not assignable to key or null or undefined. What? That's interesting. I've definitely used a symbol for a key before, but we'll just do this. Key plus one. There we go. So all that needs to happen is the key needs to be different every time. Maybe I haven't used a key for a symbol before,

02:58 or a symbol for a key, but in any case, we can reset the state of this entire app, the app implementation, by changing the key. And we've learned this before. So why aren't we doing that for handling our reset? Why do we have to add reset functionality to the use toggle itself? And here's why.

03:17 When I hit reset, no animation. And the reason for that is because we have ripped out the old switch component and we've put in a brand new one and with its initial state all set up and everything. And so while you probably make something like that work

03:36 with animated JSX and stuff like that, there are animation libraries for that, like why would you want to? It's just much easier if you're going to have a way to initialize the state of your hook or your component, and there is a use case for resetting it. There's not always, but pretty often there's a use case for resetting it.

03:54 You're going to want to do that within the hook so that things like animations and that sort of thing are taken into account. These transitions and use effects will be different in that world as well. So yeah, I typically, if I'm going to have

04:12 an initialization or a state initializer, I'm also often going to have a reset capability because even though you can technically reset the state of a component or just basically get rid of the component and get a new one with that initial state, even though you can do that, I don't really like to do it

04:31 because of these ancillary reasons.