react-async-load-script
Advanced tools
Comparing version 1.0.6 to 1.0.7
/** | ||
* Credits - https://github.com/unshiftio/one-time | ||
*/ | ||
'use strict'; | ||
'use strict' | ||
var one = require('one-time'); | ||
var one = require('one-time') | ||
@@ -16,6 +16,6 @@ /** | ||
*/ | ||
module.exports = function scripts(document, url, fn) { | ||
fn = one(fn); | ||
module.exports = function scripts (document, url, fn) { | ||
fn = one(fn) | ||
var script = document.createElement('script'); | ||
var script = document.createElement('script') | ||
@@ -27,5 +27,4 @@ /** | ||
*/ | ||
function unload() { | ||
function unload () { | ||
// fn(new Error('The script has been removed.')); | ||
// if (!script) return; | ||
@@ -41,3 +40,3 @@ // if (script.parentNode) script.parentNode.removeChild(script); | ||
// | ||
script.async = false; | ||
script.async = false | ||
@@ -49,6 +48,6 @@ // | ||
// | ||
script.onerror = function onerror() { | ||
fn(new Error('Failed to load the script.')); | ||
unload(); | ||
}; | ||
script.onerror = function onerror () { | ||
fn(new Error('Failed to load the script.')) | ||
unload() | ||
} | ||
@@ -60,6 +59,6 @@ // | ||
// | ||
script.onload = function onload() { | ||
fn(); | ||
unload(); | ||
}; | ||
script.onload = function onload () { | ||
fn() | ||
unload() | ||
} | ||
@@ -71,8 +70,8 @@ // | ||
// | ||
script.onreadystatechange = function onreadystatechange() { | ||
script.onreadystatechange = function onreadystatechange () { | ||
if (this.readyState in { loaded: 1, complete: 1 }) { | ||
fn(); | ||
unload(); | ||
fn() | ||
unload() | ||
} | ||
}; | ||
} | ||
@@ -83,6 +82,6 @@ // | ||
// | ||
(document.head || document.body).appendChild(script); | ||
script.src = url; | ||
;(document.head || document.body).appendChild(script) | ||
script.src = url | ||
return unload; | ||
}; | ||
return unload | ||
} |
11
index.js
@@ -28,3 +28,3 @@ import React from 'react' | ||
return function (WrappedComponent) { | ||
return class LoadScript extends React.Component { | ||
class LoadScript extends React.Component { | ||
constructor (props) { | ||
@@ -54,7 +54,9 @@ super(props) | ||
render () { | ||
const { forwardedRef, ...rest } = this.props | ||
return ( | ||
<WrappedComponent | ||
{...this.props} | ||
<WrappedComponent | ||
ref={forwardedRef} | ||
isScriptLoaded={this.state.isScriptLoaded} | ||
isScriptLoadSucceed={this.state.isScriptLoadSucceed} | ||
{...rest} | ||
/> | ||
@@ -64,3 +66,6 @@ ) | ||
} | ||
return React.forwardRef((props, ref) => { | ||
return <LoadScript {...props} forwardedRef={ref} /> | ||
}) | ||
} | ||
} |
@@ -28,3 +28,2 @@ /** | ||
// fn(new Error('The script has been removed.')); | ||
// if (!script) return; | ||
@@ -72,3 +71,3 @@ // if (script.parentNode) script.parentNode.removeChild(script); | ||
} | ||
}; | ||
} | ||
@@ -79,3 +78,3 @@ // | ||
// | ||
(document.head || document.body).appendChild(script); | ||
;(document.head || document.body).appendChild(script); | ||
script.src = url; | ||
@@ -82,0 +81,0 @@ |
@@ -16,5 +16,3 @@ 'use strict'; | ||
return function (WrappedComponent) { | ||
var _class, _temp; | ||
return _temp = _class = function (_React$Component) { | ||
var LoadScript = function (_React$Component) { | ||
_inherits(LoadScript, _React$Component); | ||
@@ -54,6 +52,11 @@ | ||
value: function render() { | ||
return _react2.default.createElement(WrappedComponent, _extends({}, this.props, { | ||
var _props = this.props, | ||
forwardedRef = _props.forwardedRef, | ||
rest = _objectWithoutProperties(_props, ['forwardedRef']); | ||
return _react2.default.createElement(WrappedComponent, _extends({ | ||
ref: forwardedRef, | ||
isScriptLoaded: this.state.isScriptLoaded, | ||
isScriptLoadSucceed: this.state.isScriptLoadSucceed | ||
})); | ||
}, rest)); | ||
} | ||
@@ -63,3 +66,9 @@ }]); | ||
return LoadScript; | ||
}(_react2.default.Component), _class.displayName = 'AsyncLoadScripts', _temp; | ||
}(_react2.default.Component); | ||
LoadScript.displayName = 'AsyncLoadScripts'; | ||
return _react2.default.forwardRef(function (props, ref) { | ||
return _react2.default.createElement(LoadScript, _extends({}, props, { forwardedRef: ref })); | ||
}); | ||
}; | ||
@@ -78,2 +87,4 @@ }; | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -80,0 +91,0 @@ |
{ | ||
"name": "react-async-load-script", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Async script loader HOC for React", | ||
@@ -9,3 +9,3 @@ "main": "lib/index.js", | ||
"watch": "BABEL_ENV=production babel --watch *.js --out-dir lib", | ||
"prettier": "prettier-standard 'index.js'", | ||
"prettier": "prettier-standard '*.js'", | ||
"test:watch": "yarn test --watchAll", | ||
@@ -12,0 +12,0 @@ "prepublish": "BABEL_ENV=production babel index.js --out-dir lib" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
169038
331