Skip to content

Fix/use eligible methods hook#819

Merged
HackTheW2d merged 14 commits intomainfrom
fix/use-eligible-methods-hook
Feb 13, 2026
Merged

Fix/use eligible methods hook#819
HackTheW2d merged 14 commits intomainfrom
fix/use-eligible-methods-hook

Conversation

@HackTheW2d
Copy link
Contributor

No description provided.

@HackTheW2d HackTheW2d requested a review from a team as a code owner February 11, 2026 22:35
@changeset-bot
Copy link

changeset-bot bot commented Feb 11, 2026

🦋 Changeset detected

Latest commit: 3d329a4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@paypal/react-paypal-js Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@HackTheW2d HackTheW2d marked this pull request as draft February 11, 2026 22:35
@HackTheW2d HackTheW2d marked this pull request as ready for review February 12, 2026 18:06
@HackTheW2d HackTheW2d requested a review from nityasp February 12, 2026 20:09

return () => {
isSubscribed = false;
lastFetchRef.current = null; // Reset fetch tracking on unmount or dependency change
Copy link
Contributor

@nityasp nityasp Feb 12, 2026

Choose a reason for hiding this comment

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

Instead of setting lastFetchRef.current to null, can we let the dependency array trigger new fetches when needed. That way only when payload/instance changes it triggers a new fetch.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good question. I think this fix was related to StrictMode double mounting practice.

Copy link
Contributor Author

@HackTheW2d HackTheW2d Feb 12, 2026

Choose a reason for hiding this comment

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

Hi @nityasp This is to pass our StrictMode test in react, Consider this flow:
Step: Mount 1
Without = null: fetch, lastFetchRef = {A, X}
With = null: fetch, lastFetchRef = {A, X}
────────────────────────────────────────
Step: Unmount
Without = null: isSubscribed=false, lastFetchRef = {A, X}
With = null: isSubscribed=false, lastFetchRef=null
────────────────────────────────────────
Step: Mount 2
Without = null: hasFetchedThisConfig=true, will skip fetch
With = null: hasFetchedThisConfig=false, will do a new fetch
────────────────────────────────────────
Step: Result
without = null: ❌ The first fetch's dispatch won't execute (isSubscribed=false), no data of eligibleMethods
with = null: ✅ Second fetch can dispatch and there's result stored in context

I hope this illustration can clarify your confusion. Lmk if you have any other questions. Thank you!

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for explaining it in detail. I understood it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just a minor thing. I think we don't need eligibilePaymentMethods in the dependency array. This is the output of the effect and this causes the effect to re-run everytime there is a successful fetch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Adopted the change. @EvanReinstein What do you think? I believe eligibilePaymentMethods would cause useEffect to run again(tho prevented by if check).

Copy link
Contributor

@nityasp nityasp left a comment

Choose a reason for hiding this comment

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

Left a minor comment.

Comment on lines +154 to +159
// isLoading should be true if:
// 1. We're actively fetching, OR
// 2. We don't have eligibility data yet and no error occurred
// This prevents a flash where isLoading=false before the effect runs
const isLoading =
isFetching || (!eligiblePaymentMethods && !eligibilityError);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice 👍

Copy link
Contributor

@EvanReinstein EvanReinstein left a comment

Choose a reason for hiding this comment

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

Great work on this one!

Copy link
Contributor

@nityasp nityasp left a comment

Choose a reason for hiding this comment

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

LGTM.

@HackTheW2d HackTheW2d merged commit 0cb1a60 into main Feb 13, 2026
5 checks passed
@HackTheW2d HackTheW2d deleted the fix/use-eligible-methods-hook branch February 13, 2026 20:16
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