# use-script

> React hook to load third party scripts

Latest version **3.0.0** (published 2020-10-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-script
pnpm add use-script
yarn add use-script
bun add use-script
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2020-10-28 |
| First published | 2019-11-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 59.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Seven West Media WA |
| Maintainers | jakeginnivan |

## Links

- npm: https://www.npmjs.com/package/use-script
- npm.io page: https://npm.io/package/use-script

## Dependencies (1)

- [react-is-mounted-hook](https://npm.io/package/react-is-mounted-hook.md) ^1.0.2

## Recent versions

- 3.0.0 (latest) — 2020-10-28
- 2.0.0 — 2020-02-03
- 1.0.1 — 2020-01-08
- 1.0.0 — 2020-01-08
- 0.2.0 — 2020-01-08
- 0.1.0 — 2019-11-15

## README

# useScript

Loads a third party script and returns a value when it's loaded or fails to load.

There are a few hooks out there but they do not handle concurrency or rendering multiple components which require the same script at the same time.

## Example

```ts
import { useScript } from 'use-script'

const MyComponent = () => {
    const { loading, error } = useScript({
        src: '//cdn.bootcss.com/jquery/2.2.1/jquery.min.js',
    })

    if (loading || error) {
        return null
    }

    return <OtherComponentWhichNeedsJQuery />
}
```

## Testing

JSDom based tests can't load scripts, so you can use `setTestLoadMode` to control when the script loads.

```ts
import { setTestLoadMode } from 'use-script'

// Resolve any loaded scripts immediately
setTestLoadMode(() => Promise.resolve())
```

You will need to make sure you polyfill any globals that script would create if they are relied on by your tests

---
_Source: https://npm.io/package/use-script · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
