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.

...

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


📈 36.77 Punkte
🔧 Programmierung

🔧 scriptkavi/hooks: Customizable and Open Source React Hooks


📈 36.31 Punkte
🔧 Programmierung

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


📈 36.31 Punkte
🔧 Programmierung

🔧 Understanding React Hooks: A Beginner's Guide


📈 29.76 Punkte
🔧 Programmierung

🔧 understanding react hooks


📈 29.76 Punkte
🔧 Programmierung

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


📈 29.76 Punkte
🔧 Programmierung

🔧 Understanding React’s useFormState and useFormStatus Hooks


📈 29.76 Punkte
🔧 Programmierung

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


📈 29.76 Punkte
🔧 Programmierung

🔧 Understanding React Hooks: A Comprehensive Guide


📈 29.76 Punkte
🔧 Programmierung

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


📈 28.67 Punkte
🔧 Programmierung

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


📈 28.67 Punkte
🔧 Programmierung

🔧 Optimizing React Performance with Redux and React Hooks


📈 28.67 Punkte
🔧 Programmierung

🔧 Optimizing React Performance with Redux and React Hooks


📈 28.67 Punkte
🔧 Programmierung

🔧 Optimizing React Performance with Redux and React Hooks


📈 28.67 Punkte
🔧 Programmierung

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


📈 28.03 Punkte
🔧 Programmierung

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


📈 28.03 Punkte
🔧 Programmierung

🔧 Understanding Angular Life Cycle Hooks: A Comprehensive Guide


📈 22.75 Punkte
🔧 Programmierung

🔧 Understanding Git Hooks


📈 22.75 Punkte
🔧 Programmierung

🔧 React: Understanding React's Event System


📈 22.12 Punkte
🔧 Programmierung

🔧 Understanding React.memo: Optimizing Your React Applications


📈 22.12 Punkte
🔧 Programmierung

🔧 Demystifying React Memoization: Understanding React.memo() and the useMemo hook


📈 22.12 Punkte
🔧 Programmierung

🔧 Understanding React Routing Using React Router


📈 22.12 Punkte
🔧 Programmierung

🔧 TOP 38 react hooks every developer must bookmark


📈 21.66 Punkte
🔧 Programmierung

🔧 React Custom Hooks (useRefs)


📈 21.66 Punkte
🔧 Programmierung

🔧 Don't use React.Context, create hooks.


📈 21.66 Punkte
🔧 Programmierung

🔧 Lead level: Lifecycle Methods and Hooks in React


📈 21.66 Punkte
🔧 Programmierung

🔧 Master React With These 10 Hooks 🔥


📈 21.66 Punkte
🔧 Programmierung

🔧 Fun with React and Git Hooks


📈 21.66 Punkte
🔧 Programmierung

🔧 React Hooks?


📈 21.66 Punkte
🔧 Programmierung

🔧 Efficiently Testing Asynchronous React Hooks with Vitest


📈 21.66 Punkte
🔧 Programmierung

🔧 Building an Accessible Navigation Menubar with React Hooks


📈 21.66 Punkte
🔧 Programmierung

🔧 ⚛Os 10 React Hooks Mais Úteis: 07 - useMemo⚛


📈 21.66 Punkte
🔧 Programmierung

🔧 useState() Hooks In react , How it Works ?


📈 21.66 Punkte
🔧 Programmierung

🔧 React Custom Hooks (useLocal)


📈 21.66 Punkte
🔧 Programmierung

🔧 A Beginner's Guide to React Hooks: Streamlining State and Lifecycle Management 🚀


📈 21.66 Punkte
🔧 Programmierung

matomo