Giter VIP home page Giter VIP logo

react-async-script's Introduction

React Async Script Loader

Build Status npm version Dependencies Dev 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.

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 exposeFuncs, callbackName and globalName.

  • Component: The component to wrap.
  • scriptUrl: the full of the script tag.
  • options (optional):
    • 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.
    • 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")

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

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 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); });

Inspired by react-script-loader

The build tools are highly inspired by react-bootstrap

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.