-
Notifications
You must be signed in to change notification settings - Fork 266
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
Only call element.on when the merchant passes in a callback #372
Conversation
// 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); | ||
} | ||
setElement(newElement); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes an extra render compared to before, but this seemed cleaner than modifying useAttachEvent
to return some sort of addEvent
function that is manually called here.
elementRef.current = null; | ||
if (element) { | ||
element.destroy(); | ||
setElement(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't remember the behavior of this in the later versions of react. does this throw a warning/error indicating setState being called after a component is unmounted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, removed this line, since it's a no-op.
Interestingly, I tested rendering an element, and then after a timeout, unmounting. It seems to just work — no logs/errors.
const changeEventMock = Symbol('change'); | ||
simulateEvent('change', changeEventMock); | ||
expect(mockHandler).toHaveBeenCalledWith(changeEventMock); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To test the failure scenario you were able to create repro steps for, maybe try creating a wrapper that simulates your repro steps? Can't remember the details exactly, but something like this?
const mockHandler = jest.fn();
const Wrapper = () => {
const component = useMemo(() => <CardElement onChange={mockHandler} />, []);
return component;
}
render(
<Elements stripe={mockStripe}>
<Wrapper />
</Elements>
);
const changeEventMock = Symbol('change');
simulateEvent('change', changeEventMock);
expect(mockHandler).toHaveBeenCalledWith(changeEventMock);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to do useMemo
like the repro but unfortunately didn't have much luck. However, along this same idea of copying the repro, I got a test together that leaves elements
undefined, preventing it from creating an element on the first render. This test fails on v1.16.1 but passes in 1.16.2 & here!
7b4530d
to
2caf8f0
Compare
Summary & motivation
See #360. Re-implements the functionality, fixing two issues:
Element
as a dependency for attaching the event handler, fixing [BUG]: LinkAuthenticationElement no longer firesonChange
event when it is mounted. #365Testing & documentation
onChange
event when it is mounted. #365: I verified events didn't fire correctly in v1.16.1 (version with the bug), did in v1.16.2 (latest version that reverted the bad change), and still fire correctly with these changes.