

How to load script dynamically in React
source link: https://isamrish.medium.com/how-to-load-a-dynamic-script-in-react-2940d30998dd
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to load script dynamically in React
Adding a script to the website is the common requirement. Let’s suppose that the website is using some external tools. To use it properly, you will have to use external javascript. pretty straightforward requirement, huh?
You can add script two ways — static way, just add the script tag index.html.
But there is some pitfall in this way such as
- It will increase the loading time for the website, thus affecting the website's performance.
- Script will load on other pages which don’t use the external script at all.
- You don’t have much control over when/how the script will load and execute.
Another way is to load the script is to load it dynamically.
Benefits of loading script dynamically
Some of the benefits are
- You can load script on that page on which is it really required
- It will reduce the page initial loading time
- You can have a fallback UI till the script is being loaded. It will improve make the user experience better.
- Easy and clean code
Using hook for loading it dynamically
Since our website is in React, I am going to use hooks
for loading script dynamically.
useExternalScript:
import { useEffect, useState } from "react";export const useExternalScript = (url) => {
let [state, setState] = useState(url ? "loading" : "idle");
useEffect(() => {
if (!url) {
setState("idle");
return;
} let script = document.querySelector(`script[src="${url}"]`);
const handleScript = (e) => {
setState(e.type === "load" ? "ready" : "error");
};
if (!script) {
script = document.createElement("script");
script.type = "application/javascript";
script.src = url;
script.async = true;
document.body.appendChild(script);
script.addEventListener("load", handleScript);
script.addEventListener("error", handleScript);
}
script.addEventListener("load", handleScript);
script.addEventListener("error", handleScript);
return () => {
script.removeEventListener("load", handleScript);
script.removeEventListener("error", handleScript);
};
}, [url]);
return state;
};
The above block has code that is required to use the external script. First, it will check that if the script tag is already present or not. If yes, it’s adding the event listeners for events load
and error
If the script tag is not present already, it will create a script tag, set its src
and other properties, and append it to the body. load
and error
event listeners are also added.
Here, you can see that out of the hook is state
which is a string. There are 4 possible outputs which are — loading
, idle
, load
and error
.
Now our useExternalScript
the hook is ready. Now, let’s use it in the component.
Usage of useExternalScript hook
Let’s suppose the component:
Demo:
import React from "react";
import { useExternalScript } from "./hooks/useExternalScript";
import { ComponentWithScript } from "./ComponentWithScript";export const Demo = () => {
const externalScript = "<external-script-url>";
const state = useExternalScript(externalScript);
return (
<div>
{state === "loading" && <p>Loading...</p>}
{state === "ready" && <ComponentWithScript />}
</div>
);
};
In the above component, you can see that the corresponding UI is being loaded based upon the state’s value.
Here is a working example —
I hope you like the article.
Recommend
-
9
Dynamically Importing Components with React.lazy In JavaScript frameworks like React, it's tempting to statically import a wide range of components. But not all of them are needed immediately; some components only render afte...
-
11
Dynamically Creating Script Tags With Umbrella JS By Ben Nadel on January 29, 2022 Tags: JavaScript / DHTML In the pro...
-
5
This article assumes you have basic knowledge on React and Redux. If you like to learn more about React or Redux, you can go here to learn more.
-
13
How do I dynamically generate javascript in a script tag using jade and express advertisements I am trying to dynamically g...
-
3
Load Dart Script if Dartium, Otherwise Load JavaScript in Browser February 14, 2...
-
7
[JavaScript] Load Favicon Dynamically Updated: February 23, 2015 This po...
-
10
Sometimes it is useful to load JavaScript or CSS file dynamically. In
-
12
To load (external) CSS file dynamically, insert the following LoadCSS function to your JavaScript code: function LoadCSS(url) { var ext = document.createElement('link'); ext.setAttribute("type", "text/css"); ext.s...
-
9
Introduction Some use cases require loading a specific Javascript script conditionally, this article will demonstrate how to dynamically load...
-
10
How to Lazy and Dynamically Load a Component in Angular There are three ways you can use a component in an Angular app. Loading on route change Using as a child component Dynamic...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK