Hooks at a Glance
Published: 2021-06-27 18:01:40

<h1> So, What're Hooks? <h1>

<p> Hooks are a new addition in React 16.8. They are JavaScript functions that let you use state and other React features without writing a class (Hooks don’t work inside classes). Other than that, there's little to no disadvantage.</p>

## Hook ('useState')

<p>I mentioned we can use state, well 'useState' enables you to add state to functional components. We call this Hook inside a functional component to add some local state to it. React will preserve this state between re-renders.</p>

```
const [state, setState] = useState(initialState);

```

<p> 
This'll return a stateful value, and a function(setState) for updating. For example.
</p>

```
import React, { useState } from 'react';

function Example() {
  const [total, setTotal] = useState(0);
  return (
    <div>
      <h1>clicked {total} times</h1>
      <button onClick={() => setTotal(count + 1)}>Button</button>
    </div>
  );
}
```

<p>You can hook it up as many times as you want in the same function. </p>

```
function ExampleWithManyStates() {
  // Declare multiple state variables!
  const [age, setAge] = useState(42);
  const [fruit, setFruit] = useState('banana');
  const [todos, setTodos] = useState([{ text: 'Learn Hooks' }]);
  //
}
```
<p> Looking at the equivalent for class component would just take up a lot of space, you get it.</p>

##* Hook ('useEffect')

<p> Effect Hook serves the same purpose as componentDidMount, componentDidUpdate, and componentWillUnmount in React classes, but unified into a single API(simply working as all three combined). The function passed to useEffect will be called(run) after the render is committed to the screen. In other words, by using this Hook, you tell React that your component needs to do something after render.</p>

```
import React, { useState, useEffect } from 'react';
function Example() {
  const [total, setTotal] = useState(0);

  // Similar to componentDidMount and componentDidUpdate: 
  // Update the document title using the browser API   
useEffect(() => {document.title = `clicked ${total} times`;});
  return (
    <div>
      <h1>clicked {total} times</h1>
      <button onClick={() => setTotal(total + 1)}>
        Button
      </button>
    </div>
  );
}
```
<p> So you can see, we're hitting more than a few birds with this Hookstone if you will, even if you wont...what if we want call this function when only certain value changes(update)? That is why we need second parameter which called as dependency array.
Dependency array is the second optional parameters in the useEffect function. The Effect will only executed when the second parameter value of array updated and inside the array we can put more then one value.

```
const App=()=>{
  const [value, handleChange] = useFrom({ email: "", password: ""});
  useEffect(()=>{
    console.log("render");
  }, [values.password]);

  return(
    <div>
    <>
      <input name="email" value={values.email} onChange={handleChange}/>
      <input type="password" name="password" value={values.password} onChange={handleChange} />
    </>
    </div>
   );
};
```
<p> So whenever {values.password} changes then the Effect fires off again and again.

If you put second parameter as empty array [], it will only implement componentDidMount not componentDidUpdate. So, doing so will evoke when there are any changes. In other words, re-render is not going to call the Effect anymore and render only when component Mount.</p>

##Effect Hook cleanup on isle...your code
Simply put return function inside userEffect will performs the cleanup when the component unmount which is similar to componentWillUnmount.

```
useEffect(()=>{
    console.log("render");

    return ()=>{
      console.log("unmount");
    }
  }, []);
```
Remember to clear up the old value before you try and get a new one.

##Lastly
Only call Hooks at the top level. Don’t call Hooks inside loops, conditions, or nested functions, it won't work out.