React Hooks

still confusing ?

What are Hooks

React hooks are like Anchor ( same as what ships drop in the ocean to attach the ship and ocean floor) between React state (ocean floor) and life-cycle features ( ship ) of functional components.

  • Only work with function based components, not with class based components.

  • Both arrow and regular function component works

  • Cannot nest hooks inside loops, conditions or nested functions

useState()

useState hook provides you with functionality to set state for a variable and automatically update the DOM with the new state

setState

how to import :

example useState

react_setState

In this code snippet, count and count1 will be updated both as variable as well in DOM. But count2 will always be 1 (because of +1 operation in button.onClick ) as whenever any data is changed in a react component, the whole component is re rendered. This is the reason why components exists.

Now you may ask, we can declare variables in global state and not use useState. Well declaring global variables in all programming languages are considered as bad practice except for some cases. Refer :

useState provides a consistent state without even if the component re renders.

useState for objects

Another Example useState()

useEffect()

  • executes every time a component is rendered

  • useEffect when passed no dependency works as componentDidMount

  • return arrow function from useEffect is a clean up function

  • many useEffect hook can co exists in one component

useEffect

Run the code and look at the console ... Doesn't matter whether you increment the counter or toggle the component, the whole component get re-render.

To Stop this, modify useEffect as following

Now the rendering will print on the console only when you refresh the page. Try modifying code as following

Now the component will re render only when count is updated

This is what useEffect do, to only update / rendering the component when required. There is also a way to clean up the component. Try by modifying HelloWorld.jsx

Now try to toggle switch, you the message with the component is loaded on DOM and when its unmounting. This works similiar to componentWillMount and componentWillUnmount

useRef

When you simply wants to put some html element or react component to focus

Best try running this code

useref

useReducer

Diagram explains this hook the best

userReducer

useContext

Diagram explains this hook the best

useContext

useMemo

Memo or Memoization is when you remember the result on something instead of computing it again and again when needed (until not changed)

useMemo in react is used for functions that are expensive and we dont want them to run again and again. It is similar to useEffect hook but used more for functions, whereas useEffect is used as managing state in component lifecycle, even tho they are very similiar.

useCallback

It is the useMemo alternative but for functions, rather than the result returned from them. Instead of running the function again and again. Its is mostly used along with useMemo.


Last updated

Was this helpful?