Skip to content

Commit

Permalink
feat: update forceReRender to be an array (#102)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: forceReRender prop now accepts an array.
  • Loading branch information
pedroapfilho authored and gregjopa committed Apr 11, 2021
1 parent 0734262 commit c41ee40
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
17 changes: 14 additions & 3 deletions src/components/PayPalButtons.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,19 @@ describe("<PayPalButtons />", () => {
});
});

test("should re-render Buttons when props.forceReRender changes", async () => {
test("should re-render Buttons when any item from props.forceReRender changes", async () => {
function ButtonWrapper({ initialAmount }) {
const [amount, setAmount] = useState(initialAmount);
const [currency, setCurrency] = useState("USD");
return (
<>
<button onClick={() => setAmount(amount + 1)}>
Update Amount
</button>
<PayPalButtons forceReRender={amount} />
<button onClick={() => setCurrency("EUR")}>
Update Currency
</button>
<PayPalButtons forceReRender={[amount, currency]} />
</>
);
}
Expand All @@ -125,10 +129,17 @@ describe("<PayPalButtons />", () => {

fireEvent.click(screen.getByText("Update Amount"));

// confirm re-render when the forceReRender value changes
// confirm re-render when the forceReRender value changes for first item
await waitFor(() =>
expect(window.paypal.Buttons).toHaveBeenCalledTimes(2)
);

fireEvent.click(screen.getByText("Update Currency"));

// confirm re-render when the forceReRender value changes for second item
await waitFor(() =>
expect(window.paypal.Buttons).toHaveBeenCalledTimes(3)
);
});

test("should not re-render Buttons from side-effect in props.createOrder function", async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/PayPalButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface PayPalButtonsReactProps extends PayPalButtonsComponentProps {
* Used to re-render the component.
* Changes to this prop will destroy the existing Buttons and render them again using the current props.
*/
forceReRender?: unknown;
forceReRender?: unknown[];
/**
* Pass a css class to the div container.
*/
Expand Down Expand Up @@ -47,7 +47,7 @@ export const PayPalButtons: FunctionComponent<PayPalButtonsReactProps> = ({
className = "",
disabled = false,
children = null,
forceReRender,
forceReRender = [],
...buttonProps
}: PayPalButtonsReactProps) => {
const buttonsContainerRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -131,7 +131,7 @@ export const PayPalButtons: FunctionComponent<PayPalButtonsReactProps> = ({
});

return closeButtonsComponent;
}, [isResolved, forceReRender, buttonProps.fundingSource]);
}, [isResolved, ...forceReRender, buttonProps.fundingSource]);

// useEffect hook for managing disabled state
useEffect(() => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/PayPalMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import type {
} from "@paypal/paypal-js/types/components/messages";

export interface PayPalMessagesReactProps extends PayPalMessagesComponentProps {
forceReRender?: unknown;
forceReRender?: unknown[];
className?: string;
}

export const PayPalMessages: FunctionComponent<PayPalMessagesReactProps> = ({
className = "",
forceReRender,
forceReRender = [],
...messageProps
}: PayPalMessagesReactProps) => {
const [{ isResolved, options }] = usePayPalScriptReducer();
Expand Down Expand Up @@ -64,7 +64,7 @@ export const PayPalMessages: FunctionComponent<PayPalMessagesReactProps> = ({
);
});
});
}, [isResolved, forceReRender]);
}, [isResolved, ...forceReRender]);

return <div ref={messagesContainerRef} className={className} />;
};
Expand Down
2 changes: 1 addition & 1 deletion src/stories/PayPalButtons.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const DynamicAmount = () => {
</select>
<p>Order ID: {orderID ? orderID : "unknown"}</p>

<PayPalButtons createOrder={createOrder} forceReRender={amount} />
<PayPalButtons createOrder={createOrder} forceReRender={[amount]} />
</>
);
};
Expand Down

0 comments on commit c41ee40

Please sign in to comment.