diff --git a/snippets/useScript.md b/snippets/useScript.md new file mode 100644 index 000000000..8107c0ed1 --- /dev/null +++ b/snippets/useScript.md @@ -0,0 +1,89 @@ +--- +title: useScript +tags: hooks,effect,state,event,advanced +firstSeen: 2021-10-08T05:00:00-04:00 +--- + +Dynamically loads an external script. + +- Use the `useState()` hook to create a state variable for the load status of the script. +- Use the `useEffect()` hook to handle all the logic for loading and unloading the script anytime the `src` changes. +- If no `src` value is present, set the `status` to `'idle'` and return. +- Use `Document.querySelector()` to check if a `