@newsteam/use-script
TypeScript icon, indicating that this package has built-in type declarations

0.0.12 • Public • Published

@newsteam/use-script

License npm package npm downloads

Loads a script and returns a status when the script is loaded or fails to load.

Example

import {useScript} from "@newsteam/use-script";

const MyComponent = () => {
    const script = useScript("//platform.instagram.com/en_US/embeds.js");

    if (script === "ready") {
        return <InstagramWidget />;
    }
};

Edge Cases

Sometimes a script will use an interal reference the id of the script tag and not function correctly if that id attribute is not set.

This hook supports adding an id to the script tag it generates.

import {useScript} from "@newsteam/use-script";

const MyComponent = () => {
    const script = useScript({
        src: "https://e.infogram.com/js/dist/embed-loader-min.js",
        id: "infogram-async"
    });

    if (script === "ready") {
        return <InfogramWidget />;
    }
};

Sometimes a script will modify the dom at the location of the script tag. This is common with widget embed scripts.

This hook supports appending the script tag into a custom parent instead of the head of the document.

import {useScript} from "@newsteam/use-script";

const MyComponent = ({parent}) => {
    const script = useScript({
        src: "https://e.infogram.com/js/dist/embed-loader-min.js",
        parent
    });

    if (script === "ready") {
        return <InfogramWidget />;
    }
};

Readme

Keywords

none

Package Sidebar

Install

npm i @newsteam/use-script

Weekly Downloads

0

Version

0.0.12

License

ISC

Unpacked Size

13.5 kB

Total Files

6

Last publish

Collaborators

  • richard.hazelden
  • jasonsouthin
  • dgroux
  • sweetlikepete