Skip to content

Commit dcd618c

Browse files
fix: message props update without button teardown (#536)
* fixes button teardown when message props update * chore: add changeset * fixes updateProps is not a function test script issue
1 parent 543fa08 commit dcd618c

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

.changeset/cold-mangos-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@paypal/react-paypal-js": minor
3+
---
4+
5+
Fixes Button rerendering unnecessarily when Buttons Message params change

packages/paypal-js/types/components/buttons.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,5 @@ export interface PayPalButtonsComponent {
323323
close: () => Promise<void>;
324324
isEligible: () => boolean;
325325
render: (container: HTMLElement | string) => Promise<void>;
326+
updateProps: (props: PayPalButtonsComponentOptions) => Promise<void>;
326327
}

packages/react-paypal-js/src/components/PayPalButtons.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ describe("<PayPalButtons />", () => {
349349
}
350350
return Promise.reject("Unknown error");
351351
}),
352+
updateProps: jest.fn().mockResolvedValue({}),
352353
};
353354
},
354355
version: "",
@@ -423,6 +424,7 @@ describe("<PayPalButtons />", () => {
423424
close: jest.fn().mockResolvedValue({}),
424425
isEligible: jest.fn().mockReturnValue(true),
425426
render: mockRender,
427+
updateProps: jest.fn().mockResolvedValue({}),
426428
};
427429
},
428430
version: "",

packages/react-paypal-js/src/components/PayPalButtons.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ export const PayPalButtons: FunctionComponent<PayPalButtonsComponentProps> = ({
3939
}
4040
}
4141

42-
const buttonMessageContent = JSON.stringify(buttonProps.message);
42+
if (buttons.current?.updateProps) {
43+
buttons.current.updateProps({
44+
message: buttonProps.message
45+
})
46+
}
4347

4448
// useEffect hook for rendering the buttons
4549
useEffect(() => {
@@ -125,8 +129,7 @@ export const PayPalButtons: FunctionComponent<PayPalButtonsComponentProps> = ({
125129
}, [
126130
isResolved,
127131
...forceReRender,
128-
buttonProps.fundingSource,
129-
buttonMessageContent
132+
buttonProps.fundingSource
130133
]);
131134

132135
// useEffect hook for managing disabled state

0 commit comments

Comments
 (0)