{"id":13450502,"url":"https://github.com/hupe1980/react-script-hook","last_synced_at":"2025-04-04T16:12:06.754Z","repository":{"id":34913897,"uuid":"189599615","full_name":"hupe1980/react-script-hook","owner":"hupe1980","description":"React hook to dynamically load an external script and know when its loaded","archived":false,"fork":false,"pushed_at":"2023-04-07T20:39:08.000Z","size":892,"stargazers_count":128,"open_issues_count":10,"forks_count":20,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T15:04:30.597Z","etag":null,"topics":["hook","loader","react","script"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hupe1980.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-05-31T13:36:41.000Z","updated_at":"2025-02-24T16:59:24.000Z","dependencies_parsed_at":"2024-04-11T21:48:43.775Z","dependency_job_id":"d576896d-ace4-45a0-a0b9-98d02fc47018","html_url":"https://github.com/hupe1980/react-script-hook","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hupe1980%2Freact-script-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hupe1980%2Freact-script-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hupe1980%2Freact-script-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hupe1980%2Freact-script-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hupe1980","download_url":"https://codeload.github.com/hupe1980/react-script-hook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247208142,"owners_count":20901570,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hook","loader","react","script"],"created_at":"2024-07-31T07:00:35.307Z","updated_at":"2025-04-04T16:12:06.736Z","avatar_url":"https://github.com/hupe1980.png","language":"TypeScript","funding_links":[],"categories":["Packages","TypeScript"],"sub_categories":[],"readme":"# react-script-hook\n\n\u003e React hook to dynamically load an external script and know when its loaded\n\n## Install\n\n```sh\n// with npm\nnpm install react-script-hook\n\n// with yarn\nyarn add react-script-hook\n```\n\n## How to use\n\n```javascript\nimport React from 'react';\nimport { StripeProvider } from 'react-stripe-elements';\nimport useScript from 'react-script-hook';\n\nimport MyCheckout from './my-checkout';\n\nfunction App() {\n    const [loading, error] = useScript({ src: 'https://js.stripe.com/v3/' });\n\n    if (loading) return \u003ch3\u003eLoading Stripe API...\u003c/h3\u003e;\n    if (error) return \u003ch3\u003eFailed to load Stripe API: {error.message}\u003c/h3\u003e;\n\n    return (\n        \u003cStripeProvider apiKey=\"pk_test_6pRNASCoBOKtIshFeQd4XMUh\"\u003e\n            \u003cMyCheckout /\u003e\n        \u003c/StripeProvider\u003e\n    );\n}\n\nexport default App;\n```\n\n## Use with callbacks\n\n```js\nuseScript({\n    src: 'https://js.stripe.com/v3/',\n    onload: () =\u003e console.log('Script loaded!'),\n});\n```\n\n## Check for Existing\n\nThe hook automatically handles when the script was already loaded (or started\nloading) from another instance of the hook. So you can safely add identical\n`useScript` hooks to multiple components that depend on the same external\nscript, and they will properly block on the loading of only one copy.\n\nIf you're in an environment where the script may have already been loaded in\nsome other way (not from this hook), pass an `checkForExisting` flag of `true`.\nIn this case, the hook will ensure the script is placed on the page only once\nby querying for script tags with the same `src`. Useful for SSR or SPAs with\nclient-side routing.\n\n```js\nconst [loading, error] = useScript({\n    src: 'https://js.stripe.com/v3/',\n    checkForExisting: true,\n});\n```\n\n## Conditionally calling a script\n\nIf you want to conditionally call a script you can do so by setting the src value to either the script or null. Note that the script is not removed if the src is set to null after it was previously set to a value causing the script to be appended.\n\n```js\nconst [loading, error] = useScript({\n    /** Only append stripeJS script when shouldLoadStripe is true */\n    src: shouldLoadStripe ? 'https://js.stripe.com/v3/' : null,\n});\n```\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhupe1980%2Freact-script-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhupe1980%2Freact-script-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhupe1980%2Freact-script-hook/lists"}