Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-async-script

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-async-script

A composition mixin for loading scripts asynchronously for React

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
586K
decreased by-10.55%
Maintainers
1
Weekly downloads
 
Created

What is react-async-script?

The react-async-script npm package allows you to load external scripts asynchronously in your React applications. This can be particularly useful for integrating third-party libraries or services that require script loading, such as Google Maps, payment gateways, or analytics tools.

What are react-async-script's main functionalities?

Load External Scripts

This feature allows you to load an external script asynchronously and handle the script's loading state within your React component. The example demonstrates how to use the asyncScriptLoader HOC to load a script and check its loading status.

import React from 'react';
import asyncScriptLoader from 'react-async-script';

class MyComponent extends React.Component {
  componentDidMount() {
    if (this.props.isScriptLoaded && this.props.isScriptLoadSucceed) {
      // Script loaded successfully
      console.log('Script loaded successfully');
    } else if (this.props.isScriptLoaded && !this.props.isScriptLoadSucceed) {
      // Script failed to load
      console.error('Script failed to load');
    }
  }

  render() {
    return <div>My Component</div>;
  }
}

export default asyncScriptLoader('https://example.com/external-script.js')(MyComponent);

Handle Script Load Success and Failure

This feature allows you to handle the success and failure of script loading within the componentDidUpdate lifecycle method. The example shows how to check the script's loading status and handle it accordingly.

import React from 'react';
import asyncScriptLoader from 'react-async-script';

class MyComponent extends React.Component {
  componentDidUpdate(prevProps) {
    if (prevProps.isScriptLoaded !== this.props.isScriptLoaded) {
      if (this.props.isScriptLoaded && this.props.isScriptLoadSucceed) {
        // Script loaded successfully
        console.log('Script loaded successfully');
      } else if (this.props.isScriptLoaded && !this.props.isScriptLoadSucceed) {
        // Script failed to load
        console.error('Script failed to load');
      }
    }
  }

  render() {
    return <div>My Component</div>;
  }
}

export default asyncScriptLoader('https://example.com/external-script.js')(MyComponent);

Other packages similar to react-async-script

Keywords

FAQs

Package last updated on 22 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc