Last active
December 22, 2022 10:53
-
-
Save gragland/929e42759c0051ff596bc961fb13cd93 to your computer and use it in GitHub Desktop.
React Hook recipe from https://usehooks.com. Demo: https://codesandbox.io/s/pm28k14qlj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react'; | |
// Usage | |
function App() { | |
const status = useScript( | |
'https://pm28k14qlj.codesandbox.io/test-external-script.js' | |
); | |
return ( | |
<div> | |
<div> | |
Script status: <b>{status}</b> | |
</div> | |
{status === "ready" && ( | |
<div> | |
Script function call response: <b>{TEST_SCRIPT.start()}</b> | |
</div> | |
)} | |
</div> | |
); | |
} | |
// Hook | |
function useScript(src) { | |
// Keep track of script status ("idle", "loading", "ready", "error") | |
const [status, setStatus] = useState(src ? "loading" : "idle"); | |
useEffect( | |
() => { | |
// Allow falsy src value if waiting on other data needed for | |
// constructing the script URL passed to this hook. | |
if (!src) { | |
setStatus("idle"); | |
return; | |
} | |
// Fetch existing script element by src | |
// It may have been added by another intance of this hook | |
let script = document.querySelector(`script[src="${src}"]`); | |
if (!script) { | |
// Create script | |
script = document.createElement("script"); | |
script.src = src; | |
script.async = true; | |
script.setAttribute("data-status", "loading"); | |
// Add script to document body | |
document.body.appendChild(script); | |
// Store status in attribute on script | |
// This can be read by other instances of this hook | |
const setAttributeFromEvent = (event) => { | |
script.setAttribute( | |
"data-status", | |
event.type === "load" ? "ready" : "error" | |
); | |
}; | |
script.addEventListener("load", setAttributeFromEvent); | |
script.addEventListener("error", setAttributeFromEvent); | |
} else { | |
// Grab existing script status from attribute and set to state. | |
setStatus(script.getAttribute("data-status")); | |
} | |
// Script event handler to update status in state | |
// Note: Even if the script already exists we still need to add | |
// event handlers to update the state for *this* hook instance. | |
const setStateFromEvent = (event) => { | |
setStatus(event.type === "load" ? "ready" : "error"); | |
}; | |
// Add event listeners | |
script.addEventListener("load", setStateFromEvent); | |
script.addEventListener("error", setStateFromEvent); | |
// Remove event listeners on cleanup | |
return () => { | |
if (script) { | |
script.removeEventListener("load", setStateFromEvent); | |
script.removeEventListener("error", setStateFromEvent); | |
} | |
}; | |
}, | |
[src] // Only re-run effect if script src changes | |
); | |
return status; | |
} |
used this code https://usehooks.com/useScript/ in my application, but am getting below error:
'TEST_SCRIPT' is not defined no-undef
Am getting above error here:
{status === "ready" && (
<div style={{marginTop: 20, fontSize: 20}}>
Script function call response: {TEST_SCRIPT.start()}
)}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@gragland :
If I wanted to do something like this:
Or
How could I do such a hook, with below using useScript?