React hook for loading script/SDK inside React components.
npm install react-use-sdk-hook
import useSDK from 'react-use-sdk-hook';
const MyComponent = () => {
const [loaded, cleanUp] = useSDK({src: 'https://path/to/your/desired/script.js', cleanUpFlag: true});
if (loaded) console.log('script is loaded!')
useEffect(() => {
// cleanup() removes <script /> element generated by useSDK hook
cleanUp();
}, [cleanUp]);
return (
// ...
);
};
export default MyComponent;
useSDK
recieves object with key/values below.
src
(required): source of a script thar you want to load.cleanUpFlag
(optional, default value isfalse
): ifcleanUpFlag
is true,<script />
element generated byuseSDK
hook will be removed when component unmounts.
MIT