npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-async-script-dev

v0.9.3

Published

A composition mixin for loading scripts asynchronously for React

Downloads

2

Readme

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.

Version to use

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

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

Inspired by react-script-loader

The build tools are highly inspired by react-bootstrap