1.1.2 • Published 3 years ago

use-script-loader v1.1.2

Weekly downloads
217
License
MIT
Repository
github
Last release
3 years ago

use-script-loader hook Build Status

Inspired by scriptLoader2

install

npm install use-script-loader --save

usage

const useScriptLoader = require('use-script-loader')

const [loading, script, error] = useScriptLoader('https://ajax.googleapis.com/ajax/libs/jquery/x.x.x/jquery.min.js')
if (loading) return <Spinner />
if (error) throw error
console.log(script.src);    // Prints 'https://ajax.googleapis.com/ajax/libs/jquery/x.x.x/jquery.min.js'

// use jQuery here

API

object = useScriptLoader(src, [attrs], [parentNode], [skip])

Append a <script> node with the given src URL to the <head> element in the DOM.

src

Any url that you would like to load. May be absolute or relative.

attrs (optional)

An object that contains HTML attributes to set on the <script> tag. For example, the value { id: 'hi' } would set the attribute id="hi" on the <script> tag before it is injected.

parentNode (optional)

The HTML node to which the <script> tag will be appended. If not specified, defaults to the <head> tag.

skip (optional)

If true, will not append script. Default value is false

array: [loading, script, error]

Returns an array which can be destructed to [loading, script, error]

  • loading: Remains true till script is loaded
  • script: source url
  • error: error object containing message as string

license

MIT. Copyright (c) Eesa Abid.

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago