Only re-creating script tags when src changes - see below for why attributes currently weren't skipping any effect calls anyhow
Rely on standard effect cleanup to handle unmounting. This works because
cleanup runs when component unmounts - this is how is-unmounted is implemented anyhow (so we don't need the package)
cleanup will be called for any previous value of src
Attributes currently weren't equal for any call to this hook. Objects are never strict equal unless they're the same object, never true for two reasons:
the library uses ...attributes, which creates a new object (via __rest in the compiled output)
users will pass in a new object each time if they're using React hooks correctly:
function MyComponent(props) {
useScript({ src: props.src })
}
I've aded some tests to cover unmounting and onload.
How attributes could be supported
The above demonstrates that attributes are currently not equal for any two calls. I'm unsure how often that's a use-case people want though - I'd assume most would be loading a static src, and a small % a dynamic src which is handled correctly.
However, if it was a desired feature it would be necessary to:
filter all non-serializable attributes out, and ensure a stable comparison.
or pass an explicit script-change-key which the caller could define.
Finally - the same goal could be achieved with just src by adding superfluous query strings to the src to force a change, e.g const src = "https://cdn.com/some-script?k=" + someDynamicValue.
This adds a script tag to the DOM every time the hook is mounted.
Previously this hook could be used in and out of the app without the fear of the spamming the dom with script tags.
Fixes #9 by:
src
changes - see below for why attributes currently weren't skipping any effect calls anyhowis-unmounted
is implemented anyhow (so we don't need the package)Attributes currently weren't equal for any call to this hook. Objects are never strict equal unless they're the same object, never true for two reasons:
...attributes
, which creates a new object (via__rest
in the compiled output)I've aded some tests to cover unmounting and onload.
How attributes could be supported
The above demonstrates that attributes are currently not equal for any two calls. I'm unsure how often that's a use-case people want though - I'd assume most would be loading a static src, and a small % a dynamic
src
which is handled correctly.However, if it was a desired feature it would be necessary to:
script-change-key
which the caller could define.Finally - the same goal could be achieved with just
src
by adding superfluous query strings to thesrc
to force a change, e.gconst src = "https://cdn.com/some-script?k=" + someDynamicValue
.