@@ -27,25 +27,27 @@ npm install --save react-use-scripts
27
27
28
28
## Usage
29
29
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
31
32
32
33
``` tsx
33
34
import * as React from ' react' ;
34
- import { useScript } from ' react-use-scripts' ;
35
+ import { ScriptLoader } from ' react-use-scripts' ;
35
36
36
37
const App = () => {
37
- const { ScriptLoader } = useScript ();
38
-
39
38
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 >
49
51
);
50
52
};
51
53
```
@@ -54,22 +56,28 @@ const App = () => {
54
56
55
57
```tsx
56
58
import * as React from 'react';
57
- import { useScript } from ' react-use-scripts' ;
59
+ import useScript from 'react-use-scripts';
58
60
59
61
const 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
+ };
69
73
70
74
return (
71
75
<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 >}
73
81
</div >
74
82
);
75
83
} ;
@@ -79,59 +87,54 @@ const App = () => {
79
87
80
88
## Documentation
81
89
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`;
86
91
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
+ ```
98
113
99
- - Default Props:
114
+ 2. useScript
100
115
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
+ ```
125
129
126
- - Default Props:
130
+ - Default Props:
127
131
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
+ ```
135
138
136
139
---
137
140
0 commit comments