Gathering detailed insights and metrics for @temple-wallet/react-script-hook
Gathering detailed insights and metrics for @temple-wallet/react-script-hook
Gathering detailed insights and metrics for @temple-wallet/react-script-hook
Gathering detailed insights and metrics for @temple-wallet/react-script-hook
React hook to dynamically load an external script and know when its loaded
npm install @temple-wallet/react-script-hook
Typescript
Module System
Node Version
NPM Version
TypeScript (96.97%)
JavaScript (3.03%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
142
Last Day
1
Last Week
5
Last Month
10
Last Year
58
96 Commits
5 Branches
6 Contributors
Minified
Minified + Gzipped
Latest Version
1.7.3
Package Id
@temple-wallet/react-script-hook@1.7.3
Unpacked Size
13.74 kB
Size
5.33 kB
File Count
9
NPM Version
8.19.3
Node Version
16.19.1
Publised On
27 Dec 2023
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
400%
5
Compared to previous week
Last month
400%
10
Compared to previous month
Last year
-31%
58
Compared to previous year
26
React hook to dynamically load an external script and know when its loaded
1// with npm 2npm install react-script-hook 3 4// with yarn 5yarn add react-script-hook
1import React from 'react'; 2import { StripeProvider } from 'react-stripe-elements'; 3import useScript from 'react-script-hook'; 4 5import MyCheckout from './my-checkout'; 6 7function App() { 8 const [loading, error] = useScript({ src: 'https://js.stripe.com/v3/' }); 9 10 if (loading) return <h3>Loading Stripe API...</h3>; 11 if (error) return <h3>Failed to load Stripe API: {error.message}</h3>; 12 13 return ( 14 <StripeProvider apiKey="pk_test_6pRNASCoBOKtIshFeQd4XMUh"> 15 <MyCheckout /> 16 </StripeProvider> 17 ); 18} 19 20export default App;
1useScript({
2 src: 'https://js.stripe.com/v3/',
3 onload: () => console.log('Script loaded!'),
4});
The hook automatically handles when the script was already loaded (or started
loading) from another instance of the hook. So you can safely add identical
useScript
hooks to multiple components that depend on the same external
script, and they will properly block on the loading of only one copy.
If you're in an environment where the script may have already been loaded in
some other way (not from this hook), pass an checkForExisting
flag of true
.
In this case, the hook will ensure the script is placed on the page only once
by querying for script tags with the same src
. Useful for SSR or SPAs with
client-side routing.
1const [loading, error] = useScript({
2 src: 'https://js.stripe.com/v3/',
3 checkForExisting: true,
4});
If you want to conditionally call a script you can do so by setting the src value to either the script or null. Note that the script is not removed if the src is set to null after it was previously set to a value causing the script to be appended.
1const [loading, error] = useScript({
2 /** Only append stripeJS script when shouldLoadStripe is true */
3 src: shouldLoadStripe ? 'https://js.stripe.com/v3/' : null,
4});
No vulnerabilities found.
No security vulnerabilities found.