Gathering detailed insights and metrics for @cheersjosh/react-async-script-loader
Gathering detailed insights and metrics for @cheersjosh/react-async-script-loader
Gathering detailed insights and metrics for @cheersjosh/react-async-script-loader
Gathering detailed insights and metrics for @cheersjosh/react-async-script-loader
A decorator for script lazy loading on react component
npm install @cheersjosh/react-async-script-loader
Typescript
Module System
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
256 Stars
29 Commits
55 Forks
11 Watchers
2 Branches
6 Contributors
Updated on Jun 04, 2025
Latest Version
1.0.0
Package Id
@cheersjosh/react-async-script-loader@1.0.0
Unpacked Size
23.38 kB
Size
7.73 kB
File Count
15
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
1
24
A decorator for script lazy loading on react component.
Some component may depend on other vendors which you may not want to load them until you really need them. So here it is, use High Order Component to decorate your component and it will handle lazy loading for you, it support parallel and sequential loading.
1npm install --save react-async-script-loader
1scriptLoader(...scriptSrc)([WrappedComponent])
scriptSrc
can be a string of source or an array of source. scriptSrc
will be loaded sequentially, but array of source will be loaded parallelly. It also cache the loaded script to avoid duplicated loading. More lively description see use case below.
Decorated component will receive following properties:
Name | Type | Description |
---|---|---|
isScriptLoaded | Boolean | Represent scripts loading process is over or not, maybe part of scripts load failed. |
isScriptLoadSucceed | Boolean | Represent all scripts load successfully or not. |
onScriptLoaded | Function | Triggered when all scripts load successfully. |
You can use it to decorate your component.
1import React, { Component } from 'react' 2import scriptLoader from 'react-async-script-loader' 3 4class Editor extends Component { 5 ... 6 7 componentWillReceiveProps ({ isScriptLoaded, isScriptLoadSucceed }) { 8 if (isScriptLoaded && !this.props.isScriptLoaded) { // load finished 9 if (isScriptLoadSucceed) { 10 this.initEditor() 11 } 12 else this.props.onError() 13 } 14 } 15 16 componentDidMount () { 17 const { isScriptLoaded, isScriptLoadSucceed } = this.props 18 if (isScriptLoaded && isScriptLoadSucceed) { 19 this.initEditor() 20 } 21 } 22 23 ... 24} 25 26export default scriptLoader( 27 [ 28 'https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js', 29 'https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.min.js' 30 ], 31 '/assets/bootstrap-markdown.js' 32)(Editor)
The example above means that the jquery
and marked
will be loading parallelly, and after loaded these 2 vendors, load bootstrap-markdown
sequentially.
It is possible that some script will be failed to load. ScriptLoader will cache the script that load successfully and will remove the script node which fail to load before.
Currently, if you try to reload scripts, you have to remount your component.
And it's cooler if you use decorator syntax. (ES7)
1@scriptLoader( 2 [ 3 'https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js', 4 'https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.min.js' 5 ], 6 '/assets/bootstrap-markdown.js' 7) 8class Editor extends Component { 9 10}
MIT
No vulnerabilities found.
No security vulnerabilities found.