1
1
# React Async Script Loader
2
2
3
- [ ![ Build Status] [ travis.img ]] [ travis.url ]
4
- [ ![ npm version] [ npm.img ]] [ npm.url ]
5
- [ ![ Dependencies] [ deps.img ]] [ deps.url ]
6
- [ ![ Dev Dependencies] [ devdeps.img ]] [ devdeps.url ]
3
+ [ ![ Build Status] [ travis.img ]] [ travis.url ] [ ![ npm version] [ npm.img ]] [ npm.url ] [ ![ npm downloads] [ npm.dl.img ]] [ npm.dl.url ] [ ![ Dependencies] [ deps.img ]] [ deps.url ]
7
4
8
5
A React composition mixin for loading 3rd party scripts asynchronously. This component allows you to wrap component
9
6
that needs 3rd party resources, like reCAPTCHA or Google Maps, and have them load the script asynchronously.
10
7
11
- With React 0.13, mixins are getting deprecated in favor of composition.
12
-
13
- After reading this article, [ Mixins Are Dead. Long Live Composition] [ dan_abramov ] ,
14
- I decided push react-script-loader a bit further and make a composition function that wraps component.
15
-
16
- ## Version to use
17
-
18
- - __ React < 15.5__ : v0.8.0
19
- - __ React >= 15.5__ : >= v0.9.0
20
-
21
8
## Usage
22
9
23
10
The api is very simple ` makeAsyncScriptLoader(Component, getScriptUrl, options) ` . Where options can contain exposeFuncs, callbackName and globalName.
24
11
25
- - Component: The component to wrap.
26
- - getScriptUrl: a string or function that returns the full URL of the script tag.
12
+ - ` Component ` : The component to wrap.
13
+ - ` getScriptUrl ` : a string or function that returns the full URL of the script tag.
27
14
- options * (optional)* :
28
- - exposeFuncs: Array of String . It'll create a function that will call the child component with the same name. It passes arguments and return value.
29
- - callbackName: If the scripts calls a global function when loaded, provide the callback name here. It'll be autoregistered on the window.
30
- - globalName: If wanted, provide the globalName of the loaded script. It'll be injected on the component with the same name * (ex: "grecaptcha")*
31
- - removeOnUnmount: Boolean ** default=false** : If set to true removes the script tag on the component unmount
15
+ - ` exposeFuncs ` : Array of Strings . It'll create a function that will call the child component with the same name. It passes arguments and return value.
16
+ - ` callbackName ` : If the scripts calls a global function when loaded, provide the callback name here. It'll be autoregistered on the window.
17
+ - ` globalName ` : If wanted, provide the globalName of the loaded script. It'll be injected on the component with the same name * (ex: "grecaptcha")*
18
+ - ` removeOnUnmount ` : Boolean ** default=false** : If set to true removes the script tag on the component unmount
32
19
33
20
You can retrieve the child component using the function called ` getComponent() ` .
34
21
@@ -39,7 +26,6 @@ See https://github.com/dozoisch/react-google-recaptcha
39
26
``` js
40
27
41
28
// recaptcha-wrapper.js
42
- " use strict" ;
43
29
import React from " react" ;
44
30
45
31
import ReCAPTCHA from " ./recaptcha" ;
@@ -54,8 +40,8 @@ export default makeAsyncScriptLoader(ReCAPTCHA, URL, {
54
40
globalName: globalName,
55
41
});
56
42
43
+
57
44
// main.js
58
- " use strict" ;
59
45
import React from " react" ;
60
46
import ReCAPTCHAWrapper from " ./recaptcha-wrapper.js"
61
47
@@ -104,6 +90,20 @@ let instance = ReactTestUtils.renderIntoDocument(
104
90
instance .callsACallback (function () { console .log (" Called from child" , this .constructor .displayName ); });
105
91
```
106
92
93
+ ## Notes
94
+
95
+ ### History
96
+
97
+ With React 0.13, mixins are getting deprecated in favor of composition.
98
+
99
+ After reading this article, [ Mixins Are Dead. Long Live Composition] [ dan_abramov ] ,
100
+ I decided push react-script-loader a bit further and make a composition function that wraps component.
101
+
102
+ ### Version to use
103
+
104
+ - __ React < 15.5__ : v0.8.0
105
+ - __ React >= 15.5__ : >= v0.9.0
106
+
107
107
---
108
108
109
109
* Inspired by [ react-script-loader] [ sl ] *
@@ -114,10 +114,10 @@ instance.callsACallback(function () { console.log("Called from child", this.cons
114
114
[ travis.url ] : https://travis-ci.org/dozoisch/react-async-script
115
115
[ npm.img ] : https://badge.fury.io/js/react-async-script.svg
116
116
[ npm.url ] : http://badge.fury.io/js/react-async-script
117
+ [ npm.dl.img ] : https://img.shields.io/npm/dm/react-async-script.svg
118
+ [ npm.dl.url ] : https://www.npmjs.com/package/react-async-script
117
119
[ deps.img ] : https://david-dm.org/dozoisch/react-async-script.svg
118
120
[ deps.url ] : https://david-dm.org/dozoisch/react-async-script
119
- [ devdeps.img ] : https://david-dm.org/dozoisch/react-async-script/dev-status.svg
120
- [ devdeps.url ] : https://david-dm.org/dozoisch/react-async-script#info=devDependencies
121
121
122
122
[ dan_abramov ] : https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750
123
123
[ sl ] : https://github.com/yariv/ReactScriptLoader
0 commit comments