React-stripe-elements: Clear the element in the handler

Created on 24 May 2017  路  8Comments  路  Source: stripe/react-stripe-elements

Is there a way to clear the element in the handler?

I know in stripe.js the element object has a clear function but I'm not sure how it can be called in using this module,

Most helpful comment

For those who stumble upon this issue:

The API was changed in v 1.6.0, and the onReady method should be used instead:

<CardElement onReady={(element) => this._element = element} />

All 8 comments

There's no way to do it in a React-y way right now, but what you can do is get a reference to the Element instance and call clear on it. You can use elementRef to get that reference.

(Would love any thoughts on a cleaner interface for this!)

I'm not clear on how to use elementRef. Could you please provide an example?

Hi, sorry for the delay!

Something like this:

<CardElement elementRef={(element) => this._element = element} />

And then, when you want to clear it, call this._element.clear().

(I'm going to close this for now, but if you need more help, feel free to reopen!)

For those who stumble upon this issue:

The API was changed in v 1.6.0, and the onReady method should be used instead:

<CardElement onReady={(element) => this._element = element} />

<CardElement onReady={(element) => this._element = element} /> doesn't work like expected. On render this._element loose the reference to the element and onReady doesn't seem to run on element rerender so at the end this._element will have no reference. Any work around?

The proper way to do this is to use the "useElements" hook. You can see a code sample in my comment for issue 26.
https://github.com/stripe/react-stripe-elements/issues/26#issuecomment-628758331
or...

import {useStripe, useElements, CardElement} from '@stripe/react-stripe-js'

//in component
const elements = useElements()

//in handler
const cardElement = elements.getElement(CardElement)
cardElement.clear()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cleemputc picture cleemputc  路  5Comments

shortcircuit3 picture shortcircuit3  路  5Comments

indiesquidge picture indiesquidge  路  5Comments

abachuk picture abachuk  路  3Comments

max-favilli picture max-favilli  路  5Comments