tutorials
tutorials copied to clipboard
Fix: Add cleanup function to useEffect in 03-client-step7
Description
This PR addresses an issue in the useCounterContract
hook where the useEffect
hook's recursive function could continue running even after the counterContract
value changes. This could potentially lead to memory leaks and unnecessary API calls.
Changes
- Added a cleanup function to the
useEffect
hook to set a cancellation flag (isCancelled
) when thecounterContract
value changes or the component unmounts. - The recursive
getValue
function now checks theisCancelled
flag to determine whether to continue execution.
Testing
- Verified that the recursive function stops when
counterContract
changes. - Ensured that no memory leaks or unnecessary API calls occur.
Related Issues
- Fixes potential memory leak and unnecessary API calls in
useCounterContract
.