Skip to content

Commit

Permalink
fix: message props update without button teardown (#536)
Browse files Browse the repository at this point in the history
* fixes button teardown when message props update

* chore: add changeset

* fixes updateProps is not a function test script issue
  • Loading branch information
danzhaaspaypal authored Jul 25, 2024
1 parent 543fa08 commit dcd618c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-mangos-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@paypal/react-paypal-js": minor
---

Fixes Button rerendering unnecessarily when Buttons Message params change
1 change: 1 addition & 0 deletions packages/paypal-js/types/components/buttons.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,5 @@ export interface PayPalButtonsComponent {
close: () => Promise<void>;
isEligible: () => boolean;
render: (container: HTMLElement | string) => Promise<void>;
updateProps: (props: PayPalButtonsComponentOptions) => Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ describe("<PayPalButtons />", () => {
}
return Promise.reject("Unknown error");
}),
updateProps: jest.fn().mockResolvedValue({}),
};
},
version: "",
Expand Down Expand Up @@ -423,6 +424,7 @@ describe("<PayPalButtons />", () => {
close: jest.fn().mockResolvedValue({}),
isEligible: jest.fn().mockReturnValue(true),
render: mockRender,
updateProps: jest.fn().mockResolvedValue({}),
};
},
version: "",
Expand Down
9 changes: 6 additions & 3 deletions packages/react-paypal-js/src/components/PayPalButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export const PayPalButtons: FunctionComponent<PayPalButtonsComponentProps> = ({
}
}

const buttonMessageContent = JSON.stringify(buttonProps.message);
if (buttons.current?.updateProps) {
buttons.current.updateProps({
message: buttonProps.message
})
}

// useEffect hook for rendering the buttons
useEffect(() => {
Expand Down Expand Up @@ -125,8 +129,7 @@ export const PayPalButtons: FunctionComponent<PayPalButtonsComponentProps> = ({
}, [
isResolved,
...forceReRender,
buttonProps.fundingSource,
buttonMessageContent
buttonProps.fundingSource
]);

// useEffect hook for managing disabled state
Expand Down

0 comments on commit dcd618c

Please sign in to comment.