Current section: Multiple Hooks 16 exercises
Problem

Multiple Calls to useState Breaks initialState

Transcript

00:00 So let's start with the phase stuff because this is causing a problem for us We're calling you state twice and the first time we call it we're going to get zero as our initial state and The way that we've written you state will say if our state variable hasn't been initialized yet

00:17 Then we'll initialize it. Otherwise, we won't and we'll just return what its value is And so that works nicely for the count, but then for enabled we're saying you state here's my initial value Well, it's already been initialized. And so we're not going to initialize it we'll just return what that value is and so that's why we end up with count being zero and enabled being zero because

00:36 We're just going off of what we started out as and that ends up being super weird because our set enabled When we call this with not enabled it's going to give us true and so now our count is true and our enabled is true and we click on disable and now it's false and false if We click on this now

00:55 Count is two and enables true two and everything is just a total mess. And so for this first step, we're not worried about Clicking on these different things. We're just worried about this initial Situation you'll notice that we wanted our enabled to be true But it's not it's set to zero and that's falsie

01:14 And so that's why this is saying enable and why this is initializing as disabled because that state value is zero So for this first step in this exercise We're just going to switch from this kind of implied initialization into a much more specific during this render We're initializing during this render. We're updating

01:33 So we need to update our render function to accept a phase so that we can set what phase we're in we're going to have a couple phases that we're going to be referencing and using symbols so that we have like a direct reference to those values and And that way when we render or when we when we initialize or when we update

01:53 We're rendering with that specific phase in mind so that our initial value will be count zero and enabled true So that is your task for this exercise step. Let's get into it