Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pay-later-eligibility.md
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.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ import { render, fireEvent } from "@testing-library/react";
import { PayLaterOneTimePaymentButton } from "./PayLaterOneTimePaymentButton";
import { usePayLaterOneTimePaymentSession } from "../hooks/usePayLaterOneTimePaymentSession";
import { usePayPal } from "../hooks/usePayPal";
import { useEligibleMethods } from "../hooks/useEligibleMethods";

jest.mock("../hooks/usePayLaterOneTimePaymentSession", () => ({
usePayLaterOneTimePaymentSession: jest.fn(),
}));
jest.mock("../hooks/usePayPal", () => ({
usePayPal: jest.fn(),
}));
jest.mock("../hooks/useEligibleMethods", () => ({
useEligibleMethods: jest.fn(),
}));

describe("PayLaterOneTimePaymentButton", () => {
const mockHandleClick = jest.fn();
const mockUsePayLaterOneTimePaymentSession =
usePayLaterOneTimePaymentSession as jest.Mock;
const mockUsePayPal = usePayPal as jest.Mock;
const mockUseEligibleMethods = useEligibleMethods as jest.Mock;

beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -26,9 +31,13 @@ describe("PayLaterOneTimePaymentButton", () => {
handleClick: mockHandleClick,
});
mockUsePayPal.mockReturnValue({
eligiblePaymentMethods: null,
isHydrated: true,
});
mockUseEligibleMethods.mockReturnValue({
eligiblePaymentMethods: null,
isLoading: false,
error: null,
});
});

it("should render paypal-pay-later-button when hydrated", () => {
Expand All @@ -46,7 +55,6 @@ describe("PayLaterOneTimePaymentButton", () => {

it("should render a div when not hydrated", () => {
mockUsePayPal.mockReturnValue({
eligiblePaymentMethods: null,
isHydrated: false,
});
const { container } = render(
Expand Down Expand Up @@ -162,8 +170,7 @@ describe("PayLaterOneTimePaymentButton", () => {

describe("auto-population from eligibility context", () => {
it("should auto-populate countryCode and productCode from eligibility context", () => {
mockUsePayPal.mockReturnValue({
isHydrated: true,
mockUseEligibleMethods.mockReturnValue({
eligiblePaymentMethods: {
isEligible: jest.fn().mockReturnValue(true),
getDetails: jest.fn().mockReturnValue({
Expand All @@ -172,6 +179,8 @@ describe("PayLaterOneTimePaymentButton", () => {
canBeVaulted: false,
}),
},
isLoading: false,
error: null,
});

const { container } = render(
Expand All @@ -188,9 +197,10 @@ describe("PayLaterOneTimePaymentButton", () => {
});

it("should handle when eligibility was not fetched", () => {
mockUsePayPal.mockReturnValue({
isHydrated: true,
mockUseEligibleMethods.mockReturnValue({
eligiblePaymentMethods: null,
isLoading: false,
error: null,
});

const { container } = render(
Expand All @@ -207,12 +217,13 @@ describe("PayLaterOneTimePaymentButton", () => {
});

it("should handle when PayLater details are not available", () => {
mockUsePayPal.mockReturnValue({
isHydrated: true,
mockUseEligibleMethods.mockReturnValue({
eligiblePaymentMethods: {
isEligible: jest.fn().mockReturnValue(false),
getDetails: jest.fn().mockReturnValue(undefined),
},
isLoading: false,
error: null,
});

const { container } = render(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect } from "react";

import { usePayLaterOneTimePaymentSession } from "../hooks/usePayLaterOneTimePaymentSession";
import { useEligibleMethods } from "../hooks/useEligibleMethods";
import { usePayPal } from "../hooks/usePayPal";

import type { UsePayLaterOneTimePaymentSessionProps } from "../hooks/usePayLaterOneTimePaymentSession";
Expand Down Expand Up @@ -35,7 +36,8 @@ export const PayLaterOneTimePaymentButton = ({
disabled = false,
...hookProps
}: PayLaterOneTimePaymentButtonProps): JSX.Element | null => {
const { eligiblePaymentMethods, isHydrated } = usePayPal();
const { isHydrated } = usePayPal();
const { eligiblePaymentMethods, isLoading } = useEligibleMethods();
const { error, isPending, handleClick } =
usePayLaterOneTimePaymentSession(hookProps);

Expand All @@ -49,7 +51,7 @@ export const PayLaterOneTimePaymentButton = ({
}
}, [error]);

if (isPending) {
if (isPending || isLoading) {
return null;
}

Expand Down
5 changes: 2 additions & 3 deletions packages/react-paypal-js/src/v6/hooks/useEligibleMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,12 @@ export function useEligibleMethods(
}
})
.finally(() => {
if (isSubscribed) {
setIsFetching(false);
}
setIsFetching(false);
});

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.

};
}, [
sdkInstance,
Expand Down
Loading