Socket
Socket
Sign inDemoInstall

react-async-script

Package Overview
Dependencies
6
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-async-script

A composition mixin for loading scripts asynchronously for React


Version published
Weekly downloads
620K
decreased by-7.81%
Maintainers
1
Install size
175 kB
Created
Weekly downloads
 

Readme

Source

React Async Script Loader

Build Status npm version npm downloads Dependencies

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.

Usage

The api is very simple makeAsyncScriptLoader(Component, getScriptUrl, options). Where options can contain exposeFuncs, callbackName and globalName.

  • Component: The component to wrap.
  • getScriptUrl: a string or function that returns the full URL of the script tag.
  • options (optional):
    • 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.
    • 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")
    • removeOnUnmount: Boolean default=false: If set to true removes the script tag on the component unmount

You can retrieve the child component using the function called getComponent().

Example

See https://github.com/dozoisch/react-google-recaptcha


// recaptcha-wrapper.js
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
import React from "react";
import ReCAPTCHAWrapper from "./recaptcha-wrapper.js"

function onLoad() {
  console.log("script loaded");
}

let reCAPTCHAprops = {
  siteKey: "xxxxxxx",
  //...
};

React.render(
  <ReCAPTCHAWrapper asyncScriptOnLoad={onLoad} {...reCAPTCHAprops} />,
  document.body
);

Expose Functions

This is really useful if the child component has some utility functions (like getValue) that you would like the wrapper to expose.

You can still retrieve the child component using getComponent().

Example

const MockedComponent = React.createClass({
  displayName: "MockedComponent",

  callsACallback(fn) {
    fn();
  },

  render() {
    return <span/>;
  }
});

let ComponentWrapper = makeAsyncScriptLoader(MockedComponent, "http://example.com", {
  exposeFuncs: ["callsACallback"]
});
let instance = ReactTestUtils.renderIntoDocument(
  <ComponentWrapper />
);
instance.callsACallback(function () { console.log("Called from child", this.constructor.displayName); });

Notes

History

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.

Version to use

  • React < 15.5: v0.8.0
  • React >= 15.5: >= v0.9.0

Inspired by react-script-loader

The build tools are highly inspired by react-bootstrap

Keywords

FAQs

Last updated on 04 Aug 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc