Skip to main content
Primer Checkout React Native SDK is currently in beta (v3.0.0-beta.1). The API is subject to change before the stable release.
usePrimerCardForm gives your components the card form state held by PrimerCardFormProvider: field values, update functions, validation errors, the detected network’s traits and submit(). The card inputs and PrimerCardForm use it internally. Call it yourself for a Pay button, your own fields or your own error messages.

Signature

Call it in a component below PrimerCardFormProvider, which sits inside PrimerCheckoutProvider. Without PrimerCardFormProvider it throws an error that starts with usePrimerCardForm must be used within a <PrimerCardFormProvider>. Every caller below the same provider reads and writes the same state. The options apply only to the caller that passes them.

Options

The hook keeps the latest callbacks in refs, so passing a new function on every render doesn’t cause extra calls.

Returns

Field values

Update functions

When the detected network changes, the SDK reformats the card number for the new network and cuts digits beyond its longest length.

Validation state

Submission state

Card details

Network descriptor

descriptor describes the detected network. The SDK uses it to format the card number and to size and label the CVV field. Until a network is detected, and when the native SDK has no traits for it, descriptor holds the default:

Types

Exported types: UseCardFormOptions, UsePrimerCardFormReturn, CardFormField, CardFormErrors.

When errors show

The native SDK validates the card data. The hook decides which messages to show:
  • A field’s error appears in errors after the shopper focuses the field and leaves it with a value in it. The error stays, also while the shopper edits the field again, until the value is valid. An empty field shows no error.
  • After a submit attempt, every card field with an error shows it, empty fields included. This lasts until reset().
  • isValid doesn’t depend on which errors are shown. Gate your Pay button on it.
  • The messages come from the native SDK. errors only has entries for the four card fields.
The SDK inputs call markFieldFocused and markFieldBlurred for you. Call them yourself only for fields you build.

How submit works

submit() runs these steps:
  1. It marks the submit as attempted, so every error shows.
  2. It stops when the provider isn’t ready, the card form isn’t active, or a submit is already running.
  3. It sends any pending field changes to the native SDK.
  4. It stops when isValid is false.
  5. It hands the card to the native SDK, which tokenizes it and creates the payment.
submit() never rejects. Read the result from usePrimerCheckout().paymentOutcome. The card form is active after you call start() on usePrimerPaymentMethod('PAYMENT_CARD'). Until then the SDK validates nothing and isValid stays false.

Examples

Pay button

To guard Pay, set your own paying flag on the tap and clear it only if the payment fails, as in Disable buttons during payment. error.description is for your logs. Show the shopper your own message. See PrimerError.

Your own field

Build a field with PrimerTextInput and wire it to the hook. Pass the value, the updater, the focus calls and the error:

See also

PrimerCardForm

The card fields in a fixed layout, and PrimerCardFormProvider.

Card inputs

The individual card fields and PrimerTextInput.

Card networks

BinData, the detected network and co-badged selection.

usePrimerCheckout

paymentOutcome and the rest of the checkout state.