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
69.1
Supply Chain
98.7
Quality
74.5
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
700
Last Day
1
Last Week
1
Last Month
6
Last Year
90
258 Stars
29 Commits
55 Forks
12 Watching
2 Branches
6 Contributors
Minified
Minified + Gzipped
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%
1
Compared to previous day
Last week
-66.7%
1
Compared to previous week
Last month
0%
6
Compared to previous month
Last year
-55.2%
90
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.