Skip to content
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

Fix timer cleanup on ComboBox unmount #6072

Merged
merged 5 commits into from
Apr 4, 2024

Conversation

ryo-manba
Copy link
Contributor

Closes #2154

The approach aligns with best practices for managing side effects in React, as illustrated in the official documentation.

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

🧢 Your Project:

@ryo-manba ryo-manba marked this pull request as draft March 19, 2024 06:54
@@ -276,6 +276,9 @@ const ComboBoxInput = React.forwardRef(function ComboBoxInput(props: ComboBoxInp
}

lastInputValue.current = inputValue;
return () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will run every time [isLoading, showLoading, inputValue] changes, which means we may delay certain timeouts.

Instead, this cleanup should be in its own useEffect, which runs on unmount only.

  useEffect(() => {
    return () => {
      clearTimeout(timeout.current);
      timeout.current = null;
    };
  }, []);

The addition of the timeout.current = null is needed to compensate for React StrictMode, otherwise when the second mount happens, the timer won't be set again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's a good point. Thanks for the follow-up!

@ryo-manba ryo-manba marked this pull request as ready for review March 21, 2024 13:16
@reidbarber reidbarber merged commit 884a766 into adobe:main Apr 4, 2024
25 checks passed
@ryo-manba ryo-manba deleted the fix/combobox-timer-cleanup branch April 5, 2024 01:50
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.

Unmounting ComboBox within 1/2 second when in loading state doesn't properly clean up timers
4 participants