
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
react-async-script
Advanced tools
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.
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);
The react-load-script package provides a React component for loading external scripts. It offers similar functionality to react-async-script but uses a component-based approach instead of a higher-order component (HOC). This can be more intuitive for developers who prefer using components over HOCs.
The react-helmet package allows you to manage changes to the document head, including loading external scripts. While it is more general-purpose and not specifically focused on script loading, it provides a flexible way to include external scripts along with other head elements like meta tags and title.
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.
The api is very simple makeAsyncScriptLoader(Component, scriptUrl, options)
. Where options can contain exposeFuncs, callbackName and globalName.
You can retrieve the child component using the fonction called getComponent()
.
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 ReCAPTHAWrapper from "./recaptcha-wrapper.js"
function onLoad() {
console.log("script loaded");
}
let reCAPTCHAprops = {
siteKey: "xxxxxxx",
//...
};
React.render(
<ReCAPTHAWrapper onLoad={onLoad} {...reCAPTCHAprops} />,
document.body
);
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()
.
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
0.4.0
FAQs
A composition mixin for loading scripts asynchronously for React
The npm package react-async-script receives a total of 688,538 weekly downloads. As such, react-async-script popularity was classified as popular.
We found that react-async-script demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.