Skip to main content

State management

Manage checkout state carefully to avoid re-initialization and stale data. Keep configuration objects stable and observe state changes with lifecycle-aware collectors.

Define options outside functions

Create options objects once and reuse them to avoid unnecessary re-initialization:
Define static options outside your function scope. The SDK uses deep comparison to detect actual changes, but stable object references reduce comparison overhead and improve performance.
The SDK performs deep comparison to detect actual changes in the options object. Using stable references (the GOOD pattern above) minimizes comparison overhead and remains the recommended best practice for optimal performance.

Error handling

Log diagnostics IDs and handle failures gracefully. Every error from the SDK includes identifiers that help Primer support diagnose issues.

Monitor SDK initialization

Debug configuration issues

Common debugging approaches for options-related issues:
When options aren’t working as expected, check these common issues first:
Check object reference stability:
Verify component properties vs SDK options:

Performance

Minimize unnecessary re-renders and keep configuration stable. Initialize the SDK as early as possible so it can prefetch configuration while the user navigates.

Use TypeScript interfaces for type safety

Define TypeScript interfaces for your options objects to catch errors at compile time:

Test options configuration separately

Create isolated tests for your options configuration:

Security

Never expose API keys in client code. Keep configuration minimal and handle sensitive data on your server.

Distinguish between component properties and SDK options

Component Properties are HTML attributes that configure the component container. SDK Options are configuration settings for the SDK itself.
Mixing these up will cause silent failures. Component properties must use setAttribute(). SDK options must be assigned directly to .options.
Component Properties (use setAttribute()):
  • client-token - API authentication
  • custom-styles - Visual theming
  • loader-disabled - Loader behavior
SDK Options (use options object):
  • locale - UI language
  • Payment method configuration
  • Feature settings
  • Merchant domain and API settings

Keep options simple and focused

Only configure what you need:

Testing

Use sandbox mode with test cards to verify your integration before going to production.
Test your checkout in a sandbox environment. Verify that:
  • Options are applied correctly
  • Events fire as expected
  • Error handling works for declined cards
  • All payment methods render properly