Mstar0816 / Portfolio

https://my-portfolio-ten-orpin-68.vercel.app
4 stars 0 forks source link

Memory Leaks #5

Open Mstar0816 opened 7 months ago

Mstar0816 commented 7 months ago

Leaving unused components and their associated state in memory can lead to memory leaks, causing your app’s performance to degrade over time.

Mstar0816 commented 7 months ago

Use componentWillUnmount: In class components, utilize the componentWillUnmount lifecycle method to clean up resources and subscriptions before a component is removed. class MyComponent extends React.Component { componentWillUnmount() { // Clean up resources }

render() { // Component rendering logic } }

Mstar0816 commented 7 months ago

Effect Cleanup: In functional components, return a cleanup function from the useEffect hook to handle any necessary cleanup when the component unmounts. import React, { useEffect } from 'react';

function MyComponent() { useEffect(() => { // Effect logic

return () => {
  // Cleanup logic
};

}, []);

// Component rendering logic }