Skip to content

Instantly share code, notes, and snippets.

@johnotu
Created October 17, 2020 21:59
Show Gist options
  • Save johnotu/564753d0ccaf6630edf5cfa12646eb37 to your computer and use it in GitHub Desktop.
Save johnotu/564753d0ccaf6630edf5cfa12646eb37 to your computer and use it in GitHub Desktop.
Modified useScript hook to load in a specific environment
import { useEffect } from "react";
const useScript = (url) => {
useEffect(() => {
// ensure script is only loaded in production
if (process.env.REACT_APP_MY_ENV === "production") {
const script = document.createElement("script");
script.src = url;
script.async = true;
document.body.appendChild(script);
return () => {
document.body.removeChild(script);
};
} else return;
}, [url]);
};
export default useScript;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment