Hooks
Published: 2023-10-22 21:36:42

Hooks, the bane of my existence. What are they? What do they do? How do you use them? I hope to answer all these questions here. First of all a hook is a function that lets you ‘hook into’ different react features. Think of it as an array with two different values. The first value would be a variable with the state. The second value is a variable with a handler-a function will change the current state. Something that didn’t dawn on me till writing this: values are functions, and functions are values. Here’s an example of what I mean below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/473q6po5ghzm6wfa42gd.png)

Let me explain what’s going on. 
	You are assigning the number 10 to the variable c. So when you console.log(c) it gives you the assigned number: 10.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9lg9jfs1bdk3i5hw0ge.png)

	In this case you are assigning the answer to the equation to the variable o, NOT THE EQUATION ITSELF. So when you console.log(o) you get 69.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qf4pmx4p9lagrc7gw03a.png)

	Here we are actually assigning an entire function to our variable. So this time when you console.log(d) it shows you that function and what it does.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cznph3f59cwi8um3bguw.png)

	Now finally in our last one we are assigning d() to the variable e. This time when you
 console.log(e) it gives you the string you assigned to the function assigned to d.
	With useState you can add React state to functional components, but only ones declared as a function. The hook has state kept inside it, but is accessible from the component where  you called the hook. Some Simple rules to follows: always use hooks at the top level of the React component. Don’t call them inside loops, conditions, or nested functions. Hooks must be called in the same order every time a component renders. That’s how React preserves the state of the hooks between many different useState and useEffect calls. You can not call hooks on regular JavaScript functions, only on React components and custom hooks.
	To use useState first you must import it from the the React library, then you have to invoke it inside the react component. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmjtdnab768hsbdxipg3.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlqblwfgwm5604ogl4dh.png)

Now you can call the set to update the state value or render state freely. Here’s an example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6oycfie3809drba9j0wm.png)

Here’s what is looks like in browser:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dx2qwmyrsq5ph7b754et.png)

If your variable comes from other data you don’t need state. For instance if you’re changing the theme color from dark to light based on the time of day, that can come from the system data.  A toggle is true or false and is triggered when a button is clicked, we don’t need state here either. When we want to store the input from the user or when an event is triggered, that’s when we use state. Form data is also stored in state at least until it’s sent to the database, where it can be retrieved and edited again.
	This would be the best first approach to handling multiple states at once:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsyo4q5exxvwc3zkli9p.png)

In browser:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8u8dqlbz9yjxgd1yft9.png)

Another way to do this, but is a bit messy is:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ey2i2yncknxhyq6ayht5.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgtx7g8z08i5u02wecq2.png)

This is more complex because we need to spread the existing object and then add the change or you’ll lose it.

Changing state is asynchronous and here’s an example:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3h9jw07rad6fzvt7mekp.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hj7qhfkuwz8btuxuowrj.png)

In this example I created a function handleOgresCount and I called setOgres with the new value. Doing this made it so the state wasn't updated immediately.