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
how to import :
import React, {useState} from"react";// or React.useState;
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
import React, { useState } from'react'functionApp() {const [{ counter1,counter2 },setCounter] =useState({ counter1:0, counter2:20 })return ( <divclassName='container mt-3'> <divclassName='container'> <h3>Counter1 : {counter1}</h3> <h3>Counter2 : {counter2}</h3>{/* this doesnt not work becuz whenever you update state, you need to update the whole object */}{/* Over here, we havent included the counter2 in the setCounter function. */} <buttonclassName="btn btn-primary"onClick={() =>setCounter(currentState => ({ counter1:currentState.counter1 +1 }))}>Add</button> {/* this will also not work because spread operator in objects comes first unlike in functions, where spread operator comes last. */}{/* Correct Code */} <buttonclassName="btn btn-danger"onClick={() =>setCounter(currentState => ({...currentState, counter1:currentState.counter1 -1, }))}>Subtract</button </div> </div> )}export default App;
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
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.
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.