1.0.7 • Published 6 years ago

react-async-load-script v1.0.7

Weekly downloads
225
License
ISC
Repository
-
Last release
6 years ago

React async script loader

A higher order React component to lazy load external javascript files. Scripts are loaded sequentially.

Note: Requires Promise polyfill

Installation

yarn add react-async-load-script

Usage

import scriptLoader from 'react-async-load-script'

scriptLoader([...scripts])(YourComponent)

Example

class D3Chart extends React.Component {
  componentDidUpdate (prevProps) {
    if (
      prevProps.isScriptLoadSucceed !== this.props.isScriptLoadSucceed && 
      this.props.isScriptLoadSucceed
    ) {
      // Initialize d3 chart
      console.log(d3)
    }
  }
  render () {
    return <div className='chart' />
  }
}

export default scriptLoader([
  'https://cdnjs.cloudflare.com/ajax/libs/d3/4.12.0/d3.min.js
])(D3Chart)

Properties

Your component will receive the following props

NameTypeDescription
isScriptLoadedbooleanIndicates if scripts have been loaded
isScriptLoadSuccessbooleanIndicates if all scripts are loaded without error

Test

yarn test

License

MIT

1.0.7

6 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago