@@ -27,25 +27,27 @@ npm install --save react-use-scripts
2727
2828## Usage
2929
30- - Use script tags in your ** JSX**
30+ - react-use-scripts will return a default export _ useScript_ and a named export _ { ScriptLoader }_
31+ - Use ScriptLoader as an element in your ** JSX** and add optional children and/or fallback rendering
3132
3233``` tsx
3334import * as React from ' react' ;
34- import { useScript } from ' react-use-scripts' ;
35+ import { ScriptLoader } from ' react-use-scripts' ;
3536
3637const App = () => {
37- const { ScriptLoader } = useScript ();
38-
3938 return (
40- <div >
41- <ScriptLoader
42- id = " custom-script"
43- src = " https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
44- delayMs = { 0 }
45- onCreate = { () => console .log (' created!' )}
46- type = " text/javascript"
47- />
48- </div >
39+ <ScriptLoader
40+ id = " custom-script-id"
41+ src = " https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
42+ delay = { 500 }
43+ onReady = { () => console .log (' ready!' )}
44+ onError = { (error ) => console .log (' an error has happened!' , error )}
45+ fallback = { (error ) => (
46+ <span >This has errored! { JSON .stringify (error )} </span >
47+ )}
48+ >
49+ <span >Script has loaded succesfully!
50+ </ScriptLoader >
4951 );
5052};
5153```
@@ -54,22 +56,28 @@ const App = () => {
5456
5557```tsx
5658import * as React from 'react';
57- import { useScript } from ' react-use-scripts' ;
59+ import useScript from 'react-use-scripts';
5860
5961const App = () => {
60- const { appendScript } = useScript ();
61-
62- React .useEffect (() => {
63- appendScript ({
64- id: ' script-append' ,
65- scriptText: " console.log('my script has been called')" ,
66- optionalCallback: console .log (' optional callback' ),
67- });
68- }, [appendScript ]);
62+ const [startTrigger , setStartTrigger ] = React .useState (false );
63+ const { ready , error } = useScript ({
64+ src: ' https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js' ,
65+ onReady : () => console .log (' ready!' ),
66+ onError : (error ) => console .log (' an error has happened!' , error ),
67+ startTrigger ,
68+ });
69+
70+ const handleAppendScriptClick = () => {
71+ setStartTrigger (true );
72+ };
6973
7074 return (
7175 <div >
72- <h1 >Script appended to the head programmatically!</h1 >
76+ <button onClick = { handleAppendScriptClick } >
77+ Click to start appending
78+ </button >
79+ { ready && <h1 >Script appended to the head programmatically!</h1 >}
80+ { error && <h1 >Script has errored! { JSON .stringify (error )} </h1 >}
7381 </div >
7482 );
7583} ;
@@ -79,59 +87,54 @@ const App = () => {
7987
8088## Documentation
8189
82- - ` useScript() ` returns:
83-
84- - ScriptLoader as component
85- - Props:
90+ 1. `ScriptLoader`: **all props** are optional but without either _src_ or _innerText_ this will return `null`;
8691
87- ``` tsx
88- type ScriptLoader = {
89- onCreate? : (() => null ) | undefined ; // runs after script tag rendering
90- onLoad? : (() => null ) | undefined ; // runs on script load
91- onError? : ((e : any ) => never ) | undefined ; // runs on script error
92- delayMs? : number | undefined ; // run with delayed start
93- htmlPart? : string | undefined ; // choose where to append, HEAD or BODY
94- src: string ; // script file source path
95- otherProps? : Record <string , unknown > | undefined ; // html script tag properties
96- };
97- ```
92+ ```tsx
93+ interface IScriptLoaderProps {
94+ src ?: string ;
95+ innerText ?: string ;
96+ onReady ?: () => void ;
97+ onError ?: (error : string | Event ) => void ;
98+ otherProps ?: THTMLScriptElementProps ;
99+ startTrigger ?: boolean ;
100+ id ?: string ;
101+ appendTo ?: string ;
102+ delay ?: number ;
103+ children ?:
104+ | JSX .Element
105+ | JSX .Element []
106+ | string
107+ | string []
108+ | number
109+ | number [];
110+ fallback ?: (error : string | Event ) => JSX .Element ;
111+ }
112+ ```
98113
99- - Default Props:
114+ 2. useScript
100115
101- ``` tsx
102- src : undefined ;
103- onCreate = () => null ;
104- onLoad = () => null ;
105- onError = (e ) => {
106- throw new URIError (` The script ${e .target .src } is not accessible ` );
107- };
108- delayMs = 0 ;
109- htmlPart = ' head' ;
110- otherProps : undefined ;
111- ```
112-
113- - appendScript()
114- - Props:
115-
116- ``` tsx
117- type AppendScript = {
118- id: string ; // script id
119- scriptText: string ; // script code as string
120- optionalCallback? : (() => null ) | undefined ; // optional callback function after running
121- htmlPart: string ; // choose where to append, HEAD or BODY
122- otherProps? : Record <string , unknown > | undefined ; // html script tag properties
123- };
124- ```
116+ ```tsx
117+ interface IScriptProps {
118+ src ?: string ;
119+ innerText ?: string ;
120+ onReady ?: () => void ;
121+ onError ?: (error : string | Event ) => void ;
122+ otherProps ?: THTMLScriptElementProps ;
123+ startTrigger ?: boolean ;
124+ id ?: string ;
125+ appendTo ?: string ;
126+ delay ?: number ;
127+ }
128+ ```
125129
126- - Default Props:
130+ - Default Props:
127131
128- ``` tsx
129- id : undefined ;
130- scriptText : undefined ;
131- optionalCallback = () => null ;
132- htmlPart = ' head' ;
133- otherProps = {};
134- ```
132+ ```tsx
133+ startTrigger = true,
134+ id = `react-use-script-${ new Date ().toISOString ()} `,
135+ appendTo = 'head',
136+ delay = 0,
137+ ```
135138
136139---
137140
0 commit comments