Skip to content

feat(hooks): add 'useIsMounted' #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sukvvon
Copy link
Contributor

@sukvvon sukvvon commented May 30, 2025

Overview

Checklist

  • Did you write the test code?
  • Have you run yarn run fix to format and lint the code and docs?
  • Have you run yarn run test:coverage to make sure there is no uncovered line?
  • Did you write the JSDoc?

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (bea638d) to head (49b5df8).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #244   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           33        34    +1     
  Lines          836       847   +11     
  Branches       254       258    +4     
=========================================
+ Hits           836       847   +11     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@seungrodotlee
Copy link
Member

According to our design principles, we might need to carefully consider whether providing this hook is the right approach.

@sukvvon
Copy link
Contributor Author

sukvvon commented Jun 2, 2025

@seungrodotlee

Thank you for raising that point — I completely agree with the importance of respecting React’s lifecycle and avoiding interference.

That said, I believe useIsMounted aligns with that principle rather than violating it. It doesn’t attempt to override or short-circuit any part of React’s lifecycle; instead, it acts as a guard mechanism for asynchronous side effects, helping prevent updates to unmounted components — a common source of memory leaks and hard-to-trace bugs.

In fact, this pattern complements React’s lifecycle by acknowledging the asynchronous nature of effects, especially in environments where race conditions or delayed executions (e.g., setTimeout, fetch, or requestAnimationFrame) are involved.

While it’s true that React encourages effects to be scoped correctly, in practice developers still face scenarios where external timing or delayed callbacks can lead to unintended updates. useIsMounted simply provides a safe, minimal way to check component presence without mutating state or interfering with React’s render cycle.

So rather than introducing lifecycle interference, this hook serves as a passive safety utility — one that aligns with React’s declarative philosophy by helping avoid imperative leaks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants