-
Notifications
You must be signed in to change notification settings - Fork 130
Fix/use eligible methods hook #819
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
218dc79
update useEligibleMethods hook
EvanReinstein 74db902
add useEligible Methods in PayLater button
HackTheW2d 7a46eda
clear logs
HackTheW2d 81cbb7f
chore: add changeset for PayLaterOneTimePaymentButton eligibility int…
HackTheW2d caf8b97
fix: add useEligibleMethods mock to PayLaterOneTimePaymentButton tests
HackTheW2d 414fc1e
export types, pr feedback to add isSubscribed check
HackTheW2d d7e8728
pr feedback: remove eligibilityPaymentMethods from deps, fix flash lo…
HackTheW2d afd0097
test change for isLoading changes
HackTheW2d 5c0654c
put eligible payment methods in a ref
EvanReinstein 808b819
Merge branch 'fix/use-eligible-methods-hook' of https://github.com/pa…
HackTheW2d 9a1e29a
useRef to avoid extra useEffect
HackTheW2d 3a80b00
clear logs
HackTheW2d 71d1aab
fix test
HackTheW2d 3d329a4
revert test
HackTheW2d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@paypal/react-paypal-js": patch | ||
| --- | ||
|
|
||
| Integrate useEligibleMethods hook into PayLaterOneTimePaymentButton component to automatically fetch and dispatch eligibility to context. Also fixes React Strict Mode compatibility by resetting lastFetchRef on cleanup. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,10 @@ export function useEligibleMethods( | |
| const [eligibilityError, setError] = useError(); | ||
| const [isFetching, setIsFetching] = useState(false); | ||
|
|
||
| // Use ref to access eligiblePaymentMethods in effect without adding to deps | ||
| const eligiblePaymentMethodsRef = useRef(eligiblePaymentMethods); | ||
| eligiblePaymentMethodsRef.current = eligiblePaymentMethods; | ||
|
|
||
| // Memoize payload to avoid unnecessary re-fetches when object reference changes | ||
| const memoizedPayload = useDeepCompareMemoize(payload); | ||
|
|
||
|
|
@@ -100,7 +104,10 @@ export function useEligibleMethods( | |
|
|
||
| // If eligibility exists and we haven't fetched anything yet (e.g., server hydration), | ||
| // mark as fetched to avoid unnecessary re-fetch with same payload | ||
| if (eligiblePaymentMethods && lastFetchRef.current === null) { | ||
| if ( | ||
| eligiblePaymentMethodsRef.current && | ||
| lastFetchRef.current === null | ||
| ) { | ||
| lastFetchRef.current = { | ||
| instance: sdkInstance, | ||
| payload: memoizedPayload, | ||
|
|
@@ -140,26 +147,28 @@ export function useEligibleMethods( | |
|
|
||
| return () => { | ||
| isSubscribed = false; | ||
| lastFetchRef.current = null; // Reset fetch tracking on unmount or dependency change | ||
| }; | ||
| }, [ | ||
| sdkInstance, | ||
| memoizedPayload, | ||
| eligiblePaymentMethods, | ||
| dispatch, | ||
| setError, | ||
| ]); | ||
| }, [sdkInstance, memoizedPayload, dispatch, setError]); | ||
|
|
||
| // 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); | ||
|
Comment on lines
+154
to
+159
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice 👍 |
||
|
|
||
| if (contextError) { | ||
| return { | ||
| eligiblePaymentMethods, | ||
| isLoading: isFetching, | ||
| isLoading, | ||
| error: new Error(`PayPal context error: ${contextError}`), | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| eligiblePaymentMethods, | ||
| isLoading: isFetching, | ||
| isLoading, | ||
| error: eligibilityError, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of setting
lastFetchRef.currentto null, can we let the dependency array trigger new fetches when needed. That way only when payload/instance changes it triggers a new fetch.There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.