Skip to content

Commit

Permalink
Switch back to a ref to track element creating, updating, and destroy…
Browse files Browse the repository at this point in the history
…ing (#376)

* Use elementRef for creating/updating/destroying

* test

* Update src/components/createElementComponent.tsx

Co-authored-by: Brian Mathews <93609068+bmathews-stripe@users.noreply.github.com>

* Update src/components/createElementComponent.tsx

Co-authored-by: Brian Mathews <93609068+bmathews-stripe@users.noreply.github.com>

Co-authored-by: Brian Mathews <93609068+bmathews-stripe@users.noreply.github.com>
  • Loading branch information
awalker-stripe and bmathews-stripe committed Jan 18, 2023
1 parent c68e8f6 commit 8a6c9ec
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
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

0 comments on commit 8a6c9ec

Please sign in to comment.