Current section: Multiple Hooks 16 exercises
lesson

Introduction to Multiple Hooks

Transcript

00:00 So, we can totally use multiple hooks in a single component, and the problem is that right now, that is definitely not going to work for us. If we used multiple hooks in the same component, they're both referencing the same state that we had, both the state and the set state function that we've got.

00:17 So, while a single use state is going to work because it's just one of them, we've got two and they're both referencing the same one. In our case, here in this example, we have account one and account two that could be at different values, and that is not going to work with the way that we've implemented it. What we need instead is to have a bucket that holds all of our state and set state functions

00:36 and then some mechanism for going to that bucket to retrieve the specific state and set state that we need. The trick is, there's no identifiable information in this use state call that will allow us to differentiate between one or the other.

00:51 It might be easy if we said account one and account two, and now we have an ID, and that can be how we look into that bucket to find where it is, but that's not a very ergonomic API. And so, what we need to do is have some way of identifying these individual use state

01:09 calls, and we're going to get into that in this exercise. There is a way that we can identify between these two, and it happens that the order in which these things are called is going to be our ID. We'll get into that. The other problem, though, that we need to solve first is that our initialization phase

01:29 is not very specific. We're basically just saying, if this hasn't been set before, then we're initializing, but we need to be a bit more clear about that as well to differentiate between when we're actually initializing and when we're actually updating. So this is a two-parter.

01:45 First, we're going to do the phased thing, where we're going to have different phases between initializing and updating, and then we're going to have the bucket, where we create a bucket of state so that we can store these things separate from each other, and we're going to have to generate an ID for each one of these.

02:04 So it's going to be a lot of fun. We'll see you in the exercise.