1.0.3 • Published 1 year ago
usememoryleakprevention v1.0.3
useMemoryLeakPrevention
npm install use-memory-leak-prevention
Usage
cleanupFn: A cleanup function that will be called when the component unmounts or when the dependencies change.
deps (optional): An array of dependencies that will be passed to the cleanup function. If any of the dependencies change, the cleanup function will be called. Here's an example of how you can use the useMemoryLeakPrevention hook:
import React, { useState } from 'react';
import useMemoryLeakPrevention from 'use-memory-leak-prevention';
const Component = ()=> {
const [value, setValue] = useState(0);
// Define the cleanup function
const cleanup = () => {
console.log('Cleanup function called!');
};
// Use the useMemoryLeakPrevention hook
useMemoryLeakPrevention(cleanup, [value]);
return (
<div>
<p>Value: {value}</p>
<button onClick={() => setValue(value + 1)}>Increment</button>
</div>
);
}
Conclusion
By using useMemoryLeakPrevention, you can ensure that your effect cleanup functions are properly cleaned up and prevent memory leakage in your React application.