/hooks/useScript.js
import { useEffect } from 'react';
const useScript = (url) => {
useEffect(() => {
const script = document.createElement('script');
script.src = url;
script.async = true;
document.body.appendChild(script);
return () => {
document.body.removeChild(script);
};
}, [url]);
};
export default useScript;
then
import useScript from '../hooks/useScript';
and call
useScript('/path/to/script.js');
https://stackoverflow.com/questions/34424845/adding-script-tag-to-react-jsx
https://stackoverflow.com/questions/38467574/import-javascript-file-and-call-functions-using-webpack-es6-reactjs
npm i react-helmet --save-dev
<Helmet>
<script src="/path/to/resource.js" type="text/javascript" />
<script>try{Typekit.load({ async: true });}catch(e){}</script>
</Helmet>
https://www.npmjs.com/package/react-helmet
https://www.geeksforgeeks.org/how-to-include-an-external-javascript-library-to-reactjs/
No comments:
Post a Comment