1.0.8 • Published 4 years ago
@ayltai/use-script v1.0.8
React Hook: use-script
A React hook to load a script file at runtime.
Installation
npm install @ayltai/use-script
Usage
// index.jsx
import { useScript, } from '@ayltai/use-script';
import React from 'react';
import { render, } from 'react-dom';
import { StripeProvider, } from 'react-stripe-elements';
import MyStoreCheckout from './MyStoreCheckout';
const App = () => {
const [ isLoaded, hasError, ] = useScript('https://js.stripe.com/v3/');
return (
<>
{hasError ? isLoaded ? (
<StripeProvider apiKey=''>
<MyStoreCheckout />
</StripeProvider>
) : (
<div>{`isLoaded = ${isLoaded}`}</div>
) : (
<div>{`hasError = ${hasError}`}</div>
)}
</>
);
};
render(<App />, document.getElementById('root'));
Callback
useScript('https://js.stripe.com/v3/', () => console.log(`Script loaded`));