React hook to dynamically load an external script and know when its loaded
Installations
npm install react-script-hook
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
16.13.0
NPM Version
8.3.0
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (96.76%)
JavaScript (3.24%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
hupe1980
Download Statistics
Total Downloads
38,976,629
Last Day
3,983
Last Week
3,983
Last Month
1,046,680
Last Year
13,178,216
GitHub Statistics
127 Stars
95 Commits
20 Forks
4 Watching
5 Branches
12 Contributors
Bundle Size
2.00 kB
Minified
926.00 B
Minified + Gzipped
Package Meta Information
Latest Version
1.7.2
Package Id
react-script-hook@1.7.2
Unpacked Size
13.64 kB
Size
5.30 kB
File Count
9
NPM Version
8.3.0
Node Version
16.13.0
Total Downloads
Cumulative downloads
Total Downloads
38,976,629
Last day
0%
3,983
Compared to previous day
Last week
-98.1%
3,983
Compared to previous week
Last month
8.7%
1,046,680
Compared to previous month
Last year
17%
13,178,216
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
26
react-script-hook
React hook to dynamically load an external script and know when its loaded
Install
1// with npm 2npm install react-script-hook 3 4// with yarn 5yarn add react-script-hook
How to use
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;
Use with callbacks
1useScript({
2 src: 'https://js.stripe.com/v3/',
3 onload: () => console.log('Script loaded!'),
4});
Check for Existing
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});
Conditionally calling a script
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});
License
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 3/16 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/nodejs.yml:1
- Info: no jobLevel write permissions found
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/hupe1980/react-script-hook/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/hupe1980/react-script-hook/nodejs.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 18 are checked with a SAST tool
Reason
20 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-7jxr-cg7f-gpgv
- Warn: Project is vulnerable to: GHSA-xj72-wvfv-8985
- Warn: Project is vulnerable to: GHSA-ch3r-j5x3-6q2m
- Warn: Project is vulnerable to: GHSA-p5gc-c584-jj6v
- Warn: Project is vulnerable to: GHSA-whpj-8f3w-67p5
- Warn: Project is vulnerable to: GHSA-cchq-frgv-rjh5
- Warn: Project is vulnerable to: GHSA-g644-9gfx-q4q4
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.6
/10
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-script-hook
tr-tmp-react-script-hook
React hook to dynamically load an external script and know when its loaded
@temple-wallet/react-script-hook
React hook to dynamically load an external script and know when its loaded
@custom-react-hooks/use-script
The `useScript` hook is an advanced tool for dynamically loading and managing external scripts in React applications. It supports loading multiple scripts, handling load and error events, custom script attributes, and optional script removal.
use-script
React hook to load third party scripts