stripe / react-stripe-js

React components for Stripe.js and Stripe Elements
https://stripe.com/docs/stripe-js/react
MIT License
1.78k stars 272 forks source link

Clear CardComponent #86

Closed SCasarotto closed 4 years ago

SCasarotto commented 4 years ago

Feature request or idea? Consider opening an API review!

Summary

I would like to be able to clear the CardElement after submitting data however, it doesn't appear to be a way to get a ref. I am not seeing a way to get access to elementRef (previously: https://github.com/stripe/react-stripe-elements/issues/26) and it doesn't look like createElementComponent forwards Ref. Am I missing something?

The only method I have found to work is to change the key on the CardElement component. While this works, it seems like accessing the intended properties to reset is a better solution.

Other information

dweedon-stripe commented 4 years ago

Hi @SCasarotto,

You can use useElements and elements.getElement to obtain a reference to the underlying element.

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

const PaymentForm = () => {
  const elements = useElements();

  const clearCard = () => {
    const cardElement = elements.getElement(CardElement);
    // ensure the Element is still mounted
    if (cardElement) {
      cardElement.clear()
    } 
  }

  // ...
}
SCasarotto commented 4 years ago

Oh awesome my mistake. That makes sense. Sorry long day

Safaid-Ansari commented 8 months ago

how can we clear element value in angular