DEV Community

Kiruthiga S
Kiruthiga S

Posted on

React JS

useEffect
useEffect is the one of the commonly used hook in ReactJS
Used to handle side effects in functional components

Syntax

useEffect(effectFn,[])
Enter fullscreen mode Exit fullscreen mode
useEffect(()=>{
console.log("API Calling");
},[]);
Enter fullscreen mode Exit fullscreen mode
useEffect(()=>console.log("API Calling"),[]);
Enter fullscreen mode Exit fullscreen mode

Components lifecycle
Mounting-first time call
Update-to execute again and again
Unmounting-change to another

Types of useEffect
1.empty dependency array-mounting

useEffect(() => {
  //Runs only on the first render
}, []);
Enter fullscreen mode Exit fullscreen mode

2.dependency array-update

useEffect(() => {
  //Runs on the first render
  //And any time any dependency value changes
}, [prop, state]);
Enter fullscreen mode Exit fullscreen mode

3.no dependency array-unmounting

useEffect(() => {
  //Runs on every render
});
Enter fullscreen mode Exit fullscreen mode

setInterval
The setInterval() method executes a function repeatedly at a specified interval. We can use the setInterval method in a React component to update the component's state or perform other actions.

Strict Mode
StrictMode is a react component used to identify the potential issues and bug in the react code
If strictmode is ON,the output will execute two times

Top comments (0)