A hook to dynamically load an external script and know when it's loaded.
Parameters:
Name | Type | Description |
---|---|---|
src | string | The src for the external script to load. |
checkForExisting | boolean | The hook automatically handles when the script was already loaded (or started loading) from another instance of the hook. This is for looking for the script existing in the dom from outside our code. |
attributes | object | Additional attributes. |
- Since
- 1.0.3
- Source
Returns:
Either loading or error states.
- Type:
- *
Example
import React from 'react';
import { StripeProvider } from 'react-stripe-elements';
import useScript from '@gravityforms/react-utils';
import MyCheckout from './my-checkout';
function App() {
const [loading, error] = useScript({ src: 'https://js.stripe.com/v3/' });
if (loading) return <h3>Loading Stripe API...</h3>;
if (error) return <h3>Failed to load Stripe API: {error.message}</h3>;
return (
<StripeProvider apiKey="pk_test_6pRNASCoBOKtIshFeQd4XMUh">
<MyCheckout />
</StripeProvider>
);
}
export default App;