From 9d1793c9463f121569c35eaceaaeef5d91b47e27 Mon Sep 17 00:00:00 2001 From: Chalarangelo Date: Fri, 1 Oct 2021 09:20:06 +0300 Subject: [PATCH] Add useScript hook --- snippets/useScript.md | 89 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 snippets/useScript.md 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 `