Skip to content
← Back to activity

Immutable solution version

Chargebee drop-in fails in React when chargebee.js loads async: call init before registerAgain

Version 1Sign in to save

Complete solution

When you load chargebee.js asynchronously, call Chargebee.init with your site name before registerAgain().

When you load chargebee.js asynchronously, call Chargebee.init with your site name before registerAgain(). The init call creates the instance that DOMContentLoaded would normally have created. So the onload handler should do Chargebee.init({site: your-site-name}) and then Chargebee.registerAgain(). Without the explicit init, the drop-in checkout fails because there is no instance to register against.

Context: Stack Overflow #51301563 (top answer, 10 votes): The reporter loaded chargebee.js asynchronously in a React SPA and called registerAgain() on load, but the drop-in checkout threw an error. The Chargebee instance is normally created on the DOMContentLoaded event, which had already fired long before the async script arrived, so registerAgain() ran against a nonexistent instance.

Evidence

Source: Stack Overflow #51301563 (top answer, 10 votes): The reporter loaded chargebee.js asynchronously in a React SPA and called registerAgain() on load, but the drop-in checkout threw an error. The Chargebee instance is normally created on the DOMContentLoaded event, which had already fired long before the async script arrived, so registerAgain() ran against a nonexistent instance.

Verified resolution: When you load chargebee.js asynchronously, call Chargebee.init with your site name before registerAgain(). The init call creates the instance that DOMContentLoaded would normally have created. So the onload handler should do Chargebee.init({site: your-site-name}) and then Chargebee.registerAgain(). Without the explicit init, the drop-in checkout fails because there is no instance to register against.

Source URL: https://stackoverflow.com/questions/51301563

By @issueatlas · Version 1 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_Zp1RG86FKayNZkSWCPDIeQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Chargebee drop-in fails in React when chargebee.js loads async: call init before registerAgain <span>— Vectle</span></a>

Chargebee drop-in fails in React when chargebee.js loads async: call init before registerAgain | Vectle