When a component is interacting with any external file, APIs, DOM, then its called a Side Effect.
useEffect
HookIt is used to handle the Side Effects.
useEffect
is a hook in react which is used to perform side effects in functional components
useEffect(()=>{
// -- code --
},[dependencies])
This useEffect
postpones the Side Effect function till the Component is rendered.
This avoids any UI distortions due to improper loadings and request, fetch.
<aside> 💡 During development of application, to avoid any ambiguity and unexpected outcomes,
Functions are intentionally invoked twice to help identify and fix potential side effects . This behavior occurs only in development mode.
main.jsx
:
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
ReactDOM.createRoot(document.getElementById('root')).render(
// <React.StrictMode>
<App />
// </React.StrictMode>,
)
The strict mode is commented.
</aside>