Created
October 17, 2020 21:59
-
-
Save johnotu/564753d0ccaf6630edf5cfa12646eb37 to your computer and use it in GitHub Desktop.
Modified useScript hook to load in a specific environment
This file contains hidden or 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 { 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