Lädt...

🔧 Understanding of React Hooks - useEffect.


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

What is useEffect hook?

According to the creator of react, Jordan Walke, it is introduced as way to manage side effects to functional React components. What are the side effects in functional react component? It mainly refers to the any changes that effect the program state, like modifying a variable, printing to the console, or making an API call, changing the DOM are few that considered side effects. In functional programing, minimizing side effects is important for writing predictable and maintainable code.

Syntax

useEffect is usually called at top level of your component. And required function to executed is passes as argument to useEffect, so every time component is re-render the useEffect is called.

Second argument is passed as array of dependencies. This array tells React to re-run the effect (the first argument function) whenever any of the values in this array change. In this case, the effect behaves like a combination of componentDidMount and componentDidUpdate from class components.

useEffect(() => {
        console.log('Hello World'!);
}, [])

The array of dependencies is empty then in that case the effect will only run once after the initial render of the component. else its re-runs whenever any of the values in the dependencies array change.

// Component.tsx

import { useEffect } from 'react';

export default function Component() { 

useEffect(() => {
  fetch('/api/data')
    .then(response => response.json())
    .then(data => setData(data));
}, []);
//...
}

useEffect with CleanUp

As we know that useEffect hook in React allows you to perform side effects in your functional components. It can also return a cleanup function that runs when the component unmounts, as well as before re-running the effect due to changes in dependencies.

import React, { useEffect } from 'react';

function Component() {
  useEffect(() => {
    // This is the effect
    console.log('Component mounted');

    // This is the cleanup function
    return () => {
      console.log('Component unmounted');
    };
  }, []); // Empty dependencies array means the effect runs only once

  return <div>Hey, Lary</div>;
}

export default Component;

In this example, the console.log('Component mounted') line runs once when the component mounts. The cleanup function () => console.log('Component unmounted') runs when the component unmounts.

This is useful for handling any necessary cleanup for your effects, such as cancelling API requests, or removing event listeners.

Why is Cleanups required?

useEffect(() => {
  const timer = setInterval(() => {
    setTime(prevTime => prevTime - 1);
  }, 1000);

  return () => {
    clearInterval(timer);
  };
}, []);

Cleanup is necessary in React to avoid memory leaks and unwanted behavior. When you use the useEffect hook to set up something, like an event listener or a timer, that setup is often persistent and won't automatically go away when the component unmounts. it's not tied to the lifecycle of the component. for example, if you start a timer with setInterval, the timer will continue to run and call its callback function at the specified interval, even if the component that started it has unmounted. The browser doesn't know that the component has unmounted and that it should stop the timer.

That concludes the fundamentals of useEffect. If you enjoyed the article, please share it with your friends and the community. I will put out content a further article about useContext soon.

For additional articles, you can visit the blog.viditkushwaha.

...

🔧 🪝Mastering React Hooks Transform Your React Development with Hooks🚀


📈 35.93 Punkte
🔧 Programmierung

🔧 React Hooks - Top7 Most Commonly Used React Hooks for Efficient Component Management


📈 35.93 Punkte
🔧 Programmierung

🔧 Advanced React Hooks: Custom Hooks and Performance Optimization


📈 30.53 Punkte
🔧 Programmierung

🔧 Advanced React Hooks: Custom Hooks and Performance Optimization


📈 30.53 Punkte
🔧 Programmierung

🔧 The Essential Rules of Hooks in React: How to Use Hooks Properly


📈 30.53 Punkte
🔧 Programmierung

🔧 Learn React Hooks – Common Hooks Explained with Code Examples


📈 30.53 Punkte
🔧 Programmierung

🔧 scriptkavi/hooks: Customizable and Open Source React Hooks


📈 30.53 Punkte
🔧 Programmierung

🔧 Bloodline of Hooks: Custom Hooks in React for Advanced Devs


📈 30.53 Punkte
🔧 Programmierung

🔧 🧠 Understanding React Hooks – The Modern Way to Write React


📈 29.57 Punkte
🔧 Programmierung

🔧 Unlocking React's Power: Understanding React's Core Hooks


📈 29.57 Punkte
🔧 Programmierung

🔧 Understanding React Hooks: How to Use useRef, useMemo, and useCallback for More Efficient Code


📈 24.16 Punkte
🔧 Programmierung

🔧 Understanding React Hooks: A Comprehensive Guide


📈 24.16 Punkte
🔧 Programmierung

🔧 Understanding React 19 New Hooks


📈 24.16 Punkte
🔧 Programmierung

🔧 Understanding React Hooks: A Beginner’s Guide


📈 24.16 Punkte
🔧 Programmierung

🔧 Understanding React Hooks: Simplify State and Side Effects in Your Apps


📈 24.16 Punkte
🔧 Programmierung

🔧 Level Up Your React Skills: Understanding and Using Hooks


📈 24.16 Punkte
🔧 Programmierung

🔧 Level Up Your React Skills: Understanding and Using Hooks


📈 24.16 Punkte
🔧 Programmierung

🔧 Understanding React Hooks: A Beginner's Guide


📈 24.16 Punkte
🔧 Programmierung

🔧 understanding react hooks


📈 24.16 Punkte
🔧 Programmierung

🔧 Understanding State Management in React: Avoiding Pitfalls with Custom Hooks


📈 24.16 Punkte
🔧 Programmierung

🔧 Understanding React’s useFormState and useFormStatus Hooks


📈 24.16 Punkte
🔧 Programmierung

🔧 React Js Part 5 : Lifecycle Methods and Hooks in React


📈 23.37 Punkte
🔧 Programmierung

🔧 Mastering React: A Guide to the Most Important React Hooks


📈 23.37 Punkte
🔧 Programmierung

🔧 Form Validation in React: An In-Depth Tutorial with Hooks and React Hook Form


📈 23.37 Punkte
🔧 Programmierung

🔧 Optimizing React Performance with Redux and React Hooks


📈 23.37 Punkte
🔧 Programmierung

🔧 Optimizing React Performance with Redux and React Hooks


📈 23.37 Punkte
🔧 Programmierung

🔧 Optimizing React Performance with Redux and React Hooks


📈 23.37 Punkte
🔧 Programmierung

🔧 Revolutionizing React: Unveiling the New Hooks in React 19🚀


📈 23.37 Punkte
🔧 Programmierung

🔧 React Hooks Tutorial: A Beginner’s Guide to Modern React Development


📈 23.37 Punkte
🔧 Programmierung

🔧 Using React Hooks in React Native: Best Practices


📈 23.37 Punkte
🔧 Programmierung

🔧 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 21.61 Punkte
🔧 Programmierung

🔧 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 21.61 Punkte
🔧 Programmierung

🔧 Understanding Angular Life Cycle Hooks


📈 18.76 Punkte
🔧 Programmierung