Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch back to a ref to track element creating, updating, and destroying #376

Merged
merged 4 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions src/components/createElementComponent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,34 @@ describe('createElementComponent', () => {
expect(simulateOff).not.toBeCalled();
});

it('creates, destroys, then re-creates element in strict mode', () => {
expect.assertions(4);

let elementCreated = false;

mockElements.create.mockImplementation(() => {
expect(elementCreated).toBe(false);
elementCreated = true;

return mockElement;
});

mockElement.destroy.mockImplementation(() => {
elementCreated = false;
});

render(
<StrictMode>
<Elements stripe={mockStripe}>
<CardElement />
</Elements>
</StrictMode>
);

expect(mockElements.create).toHaveBeenCalledTimes(2);
expect(mockElement.destroy).toHaveBeenCalledTimes(1);
});

it('mounts the element', () => {
const {container} = render(
<Elements stripe={mockStripe}>
Expand Down
23 changes: 15 additions & 8 deletions src/components/createElementComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const createElementComponent = (
const [element, setElement] = React.useState<stripeJs.StripeElement | null>(
null
);
const elementRef = React.useRef<stripeJs.StripeElement | null>(null);
const domNode = React.useRef<HTMLDivElement | null>(null);

const {setCart, setCartState} = useCartElementContextWithUseCase(
Expand Down Expand Up @@ -138,21 +139,26 @@ const createElementComponent = (
useAttachEvent(element, 'checkout', checkoutCallback);

React.useLayoutEffect(() => {
if (element === null && elements && domNode.current !== null) {
if (elementRef.current === null && elements && domNode.current !== null) {
const newElement = elements.create(type as any, options);
if (type === 'cart' && setCart) {
// we know that elements.create return value must be of type StripeCartElement if type is 'cart',
// we need to cast because typescript is not able to infer which overloaded method is used based off param type
setCart((newElement as unknown) as stripeJs.StripeCartElement);
}

// Store element in a ref to ensure it's _immediately_ available in cleanup hooks in StrictMode
elementRef.current = newElement;
// Store element in state to facilitate event listener attachment
setElement(newElement);

newElement.mount(domNode.current);
}
}, [element, elements, options, setCart]);
}, [elements, options, setCart]);

const prevOptions = usePrevious(options);
React.useEffect(() => {
if (!element) {
if (!elementRef.current) {
return;
}

Expand All @@ -161,17 +167,18 @@ const createElementComponent = (
]);

if (updates) {
element.update(updates);
elementRef.current.update(updates);
}
}, [options, prevOptions, element]);
}, [options, prevOptions]);

React.useLayoutEffect(() => {
return () => {
if (element) {
element.destroy();
if (elementRef.current) {
elementRef.current.destroy();
elementRef.current = null;
}
};
}, [element]);
}, []);

return <div id={id} className={className} ref={domNode} />;
};
Expand Down