Package Exports
- react-async-script
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-async-script) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
React Async Script Loader
A React composition mixin for loading 3rd party scripts asynchronously. This component allows you to wrap component that needs 3rd party resources, like reCAPTCHA or Google Maps, and have them load the script asynchronously.
With React 0.13, mixins are getting deprecated in favor of composition.
After reading this article, Mixins Are Dead. Long Live Composition, I decided push react-script-loader a bit further and make a composition function that wraps component.
Usage
The api is very simple makeAsyncScriptLoader(Component, scriptUrl, options)
. Where options can contain callbackName and globalName.
- Component: The component to wrap.
- scriptUrl: the full of the script tag.
- options (optional):
- callbackName: If the scripts calls a global function when loaded, provide the callback name here. It'll be autoregistered on the window.
- globalName: If wanted, provide the globalName of the loaded script. It'll be injected on the component with the same name (ex: "grecaptcha")
Example
See https://github.com/dozoisch/react-google-recaptcha
// recaptcha-wrapper.js
"use strict";
import React from "react";
import ReCAPTCHA from "./recaptcha";
import makeAsyncScriptLoader from "./react-async-script";
const callbackName = "onloadcallback";
const URL = `https://www.google.com/recaptcha/api.js?onload=${callbackName}&render=explicit`;
const globalName = "grecaptcha";
export default makeAsyncScriptLoader(ReCAPTCHA, URL, {
callbackName: callbackName,
globalName: globalName,
});
// main.js
"use strict";
import React from "react";
import ReCAPTHAWrapper from "./recaptcha-wrapper.js"
function onLoad() {
console.log("script loaded");
}
let reCAPTCHAprops = {
siteKey: "xxxxxxx",
//...
};
React.render(
<ReCAPTHAWrapper onLoad={onLoad} {...reCAPTCHAprops} />,
document.body
);
Inspired by react-script-loader
The build tools are highly inspired by react-bootstrap