Using Lemon.js with Frameworks and Libraries
Modern JS frameworks and libraries may require a different approach to integrating Lemon.js.
Next.js
To optimize the loading of the script, use the Next.js inbuilt next/script
Script component. By default the strategy
is set to afterInteractive
, which is the recommended strategy for Lemon.js.
From Next.js documentation:
Scripts that use the
afterInteractive
strategy are injected into the HTML client-side and will load after some (or all) hydration occurs on the page. This is the default strategy of the Script component and should be used for any script that needs to load as soon as possible but not before any first-party Next.js code.
Embed Lemon.js in Next.js
Add your Lemon Squeezy button
Replace the above link with your actual product link.
Final code
React
In React, you can use the useEffect
hook to load Lemon.js. This will ensure that Lemon.js is loaded only once when the component mounts.
Create a new component and use the useEffect
hook to load Lemon.js.
In more recent versions of React you can use the <script>
component. For more information, refer to the official React documentation for adding external script to a website.
Vue.js
In Vue.js, you can use the onMounted()
lifecycle hook to load Lemon.js. This will ensure that Lemon.js is loaded only once when the component mounts.
TypeScript
To enable code completion in TypeScript for Lemon.js, declare Lemon.js methods in a shims.d.ts
file, ensuring TypeScript recognition.
Re-initialize button listeners
Upon loading Lemon.js, this script automatically initializes and listens for .lemonsqueezy-button
clicks.
However, you may encounter situations where Lemon.js initializes before your component has rendered. In such cases, manually call window.createLemonSqueezy()
after the component has mounted and Lemon.js is loaded to retrigger button event listeners.
This method can be called multiple times as needed.
For example, in React: