A collection of reusable and well-documented custom React hooks for supercharging your React applications. These hooks cover a wide range of functionalities, making it easier for you to build dynamic and interactive user interfaces.
Installations
npm install @custom-react-hooks/use-script
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
18.17.0
NPM Version
10.3.0
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (96.96%)
JavaScript (2.56%)
Shell (0.47%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Download Statistics
Total Downloads
9,759
Last Day
4
Last Week
4
Last Month
882
Last Year
9,081
GitHub Statistics
20 Stars
36 Commits
1 Forks
1 Watching
1 Branches
1 Contributors
Bundle Size
875.00 B
Minified
491.00 B
Minified + Gzipped
Package Meta Information
Latest Version
1.5.1
Package Id
@custom-react-hooks/use-script@1.5.1
Unpacked Size
8.36 kB
Size
3.82 kB
File Count
8
NPM Version
10.3.0
Node Version
18.17.0
Publised On
18 Aug 2024
Total Downloads
Cumulative downloads
Total Downloads
9,759
Last day
0%
4
Compared to previous day
Last week
-97.9%
4
Compared to previous week
Last month
12.4%
882
Compared to previous month
Last year
1,239.4%
9,081
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
useScript Hook
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.
Features
- Multiple Script Support: Can handle an array of script sources.
- Event Callbacks: Provides
onLoad
andonError
callbacks for handling respective script events. - Custom Script Attributes: Allows setting attributes like
defer
,async
, or customdata-*
attributes. - Optional Script Removal: Can remove script tags from the DOM on component unmount.
Installation
Installing Only Current Hooks
1npm install @custom-react-hooks/use-script
or
1yarn add @custom-react-hooks/use-script
Installing All Hooks
1npm install @custom-react-hooks/all
or
1yarn add @custom-react-hooks/all
Importing the Hook
The useScript
hook must be imported using a named import as shown below:
Named Import:
1import { useScript } from '@custom-react-hooks/use-script';
This approach ensures that the hook integrates seamlessly into your project, maintaining consistency and predictability in how you use our package.
Usage
Import the useScript
hook and use it in your React components. You can specify multiple scripts and custom attributes:
1import { useScript } from '@custom-react-hooks/all'; 2 3const ScriptComponent = () => { 4 const status = useScript( 5 'https://cdn.jsdelivr.net/npm/canvas-confetti@1.4.0/dist/confetti.browser.js', 6 ); 7 8 const triggerConfetti = () => { 9 if (status === 'ready' && window.confetti) { 10 window.confetti({ 11 particleCount: 100, 12 spread: 70, 13 origin: { y: 0.6 }, 14 }); 15 } 16 }; 17 18 return ( 19 <div> 20 <h1>Confetti Script Loader</h1> 21 22 <p>Script Loading Status: {status}</p> 23 24 <button 25 onClick={triggerConfetti} 26 disabled={status !== 'ready'} 27 > 28 Trigger Confetti 29 </button> 30 {status === 'error' && ( 31 <p>Failed to load the script. Please check the URL.</p> 32 )} 33 </div> 34 ); 35}; 36 37export default ScriptComponent;
In this example, the hook loads multiple scripts with additional attributes and provides callbacks for load and error events.
API Reference
Parameters
src
: A string or an array of strings representing the script source URLs.options
: An object containing:onLoad
: Callback function triggered when the script loads.onError
: Callback function triggered on script load error.removeOnUnmount
: Boolean indicating whether to remove the script tags on unmount.- Additional attributes (e.g.,
defer
,async
) to be set on the script tags.
Returns
src
: Script source URL.status
: Load status of the script ('loading'
,'ready'
, or'error'
).
Use Cases
- Third-Party Integrations: Load external libraries or widgets, like social media sharing buttons or analytics scripts.
- Conditional Script Loading: Load scripts only when certain conditions are met, optimizing performance.
- Feature Enhancement: Enhance your application with additional features available via external scripts.
- Asynchronous Script Loading: Manage asynchronous loading of scripts without blocking the rendering of your application.
Contributing
Contributions to enhance the useScript
hook are welcome. Feel free to submit issues or pull requests to improve its functionality and usability.
No vulnerabilities found.
No security vulnerabilities found.