Skip to content

Commit

Permalink
useLayoutEffect for synchronous cleanup (#156)
Browse files Browse the repository at this point in the history
Fixes #154 on react17
  • Loading branch information
thetrevdev authored Feb 1, 2021
1 parent 1ad46a5 commit e304667
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/components/createElementComponent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,33 @@ describe('createElementComponent', () => {
false
);


it('Can remove and add CardElement at the same time', () => {
let cardMounted = false;
mockElement.mount.mockImplementation(()=> {
if(cardMounted){
throw new Error('Card already mounted');
}
cardMounted = true;
});
mockElement.destroy.mockImplementation(()=> {
cardMounted = false;
});

const {rerender} = render(
<Elements stripe={mockStripe}>
<CardElement key={"1"} />
</Elements>
);
rerender(
<Elements stripe={mockStripe}>
<CardElement key={"2"} />
</Elements>
);

expect(mockElement.mount).toHaveBeenCalledTimes(2);
});

it('gives the element component a proper displayName', () => {
expect(CardElement.displayName).toBe('CardElement');
});
Expand Down
2 changes: 1 addition & 1 deletion src/components/createElementComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const createElementComponent = (
}
}, [options]);

React.useEffect(() => {
React.useLayoutEffect(() => {
return () => {
if (elementRef.current) {
elementRef.current.destroy();
Expand Down

0 comments on commit e304667

Please sign in to comment.