Skip to content

Commit fb58d3b

Browse files
committed
docs(readme): update readme
1 parent 30ba4e0 commit fb58d3b

File tree

2 files changed

+84
-46
lines changed

2 files changed

+84
-46
lines changed

README.md

Lines changed: 83 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,88 +18,126 @@
1818
## Install
1919

2020
```bash
21-
npm install --save react-use-hotjar
21+
npm install --save react-use-script
2222
```
2323

2424
---
2525

2626
## Usage
2727

28-
- Initializing Hotjar (use it at your very `index.jsx`)
28+
- Use script tags in your **JSX**
2929

3030
```tsx
3131
import * as React from 'react';
32+
import { useScript } from 'react-use-script';
33+
34+
const App = () => {
35+
const { ScriptLoader } = useScript();
36+
37+
return (
38+
<div>
39+
<ScriptLoader
40+
id="custom-script"
41+
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
42+
delayMs={0}
43+
onCreate={() => console.log('created!')}
44+
type="text/javascript"
45+
/>
46+
</div>
47+
);
48+
};
49+
```
3250

33-
import { useHotjar } from 'react-use-hotjar';
51+
- Append scripts to the document programmatically
3452

35-
const myCustomLogger = console.info;
53+
```tsx
54+
import * as React from 'react';
55+
import { useScript } from 'react-use-script';
3656

37-
const HotjarReadyApp = () => {
38-
const { initHotjar } = useHotjar();
57+
const App = () => {
58+
const { appendScript } = useScript();
3959

4060
React.useEffect(() => {
41-
initHotjar(hotjarId, hotjarVersion, myCustomLogger);
42-
});
43-
44-
return <App />;
61+
appendScript({
62+
id: 'script-append',
63+
scriptText: "console.log('my script has been called')",
64+
optionalCallback: console.log('optional callback'),
65+
});
66+
}, [appendScript]);
67+
68+
return (
69+
<div>
70+
<h1>Script appended to the head programmatically!</h1>
71+
</div>
72+
);
4573
};
4674
```
4775

48-
- Identifying Users (use it wherever you get access to user's information)
76+
---
77+
78+
## Documentation
79+
80+
`useScript()` returns:
81+
82+
1. <ScriptLoader />
83+
84+
- Props
4985

5086
```tsx
51-
const MyCustomComponent = () => {
52-
const { initHotjar } = useHotjar();
53-
54-
const handleUserInfo = (userInfo) => {
55-
const { id, ...restUserInfo } = userInfo;
56-
57-
identifyHotjar(
58-
id,
59-
JSON.stringify({
60-
restUserInfo,
61-
})
62-
);
63-
};
87+
type ScriptLoader = {
88+
onCreate?: (() => null) | undefined; // runs after script tag rendering
89+
onLoad?: (() => null) | undefined; // runs on script load
90+
onError?: ((e: any) => never) | undefined; // runs on script error
91+
delayMs?: number | undefined; // run with delayed start
92+
htmlPart?: string | undefined; // choose where to append, HEAD or BODY
93+
src: string; // script file source path
94+
otherProps?: Record<string, unknown> | undefined; // html script tag properties
6495
};
6596
```
6697

67-
---
98+
- Default Props
6899

69-
## Documentation
70-
71-
`useHotjar()` returns:
100+
```tsx
101+
src: undefined;
102+
onCreate = () => null;
103+
onLoad = () => null;
104+
onError = (e) => {
105+
throw new URIError(`The script ${e.target.src} is not accessible`);
106+
};
107+
delayMs = 0;
108+
htmlPart = 'head';
109+
otherProps: undefined;
110+
```
72111

73-
- initHotjar()
112+
2. appendScript()
74113

75-
1. `hotjarId`: Your Hotjar application ID ex.: 1933331
76-
2. `hotjarVersion`: Hotjar's current version ex.: 6
77-
3. `logCallback`: Optional callback for logging wether Hotjar is ready or not
114+
- Props
78115

79116
```tsx
80-
initHotjar: (
81-
hotjarId: string,
82-
hotjarVersion: string,
83-
logCallback?: () => void
84-
) => boolean;
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+
};
85124
```
86125

87-
- identifyHotjar()
88-
89-
1. `userId`: Unique user's identification as string
90-
2. `userInfo`: Stringfied user info of key-value pairs (note this must not be so long and deep according to [docs](https://help.hotjar.com/hc/en-us/articles/360033640653-Identify-API-Reference))
91-
3. `logCallback`: Optional callback for logging wether Hotjar identified user or not
126+
- Default Props
92127

93128
```tsx
94-
identifyHotjar: (userId: string, userInfo: string, logCallback?: () => void) =>
95-
boolean;
129+
id: undefined;
130+
scriptText: undefined;
131+
optionalCallback = () => null;
132+
htmlPart = 'head';
133+
otherProps = {};
96134
```
97135

98136
---
99137

100138
## License
101139

102-
react-use-hotjar is [MIT licensed](./LICENSE).
140+
react-use-script is [MIT licensed](./LICENSE).
103141

104142
---
105143

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-use-script",
3-
"version": "1.0.6",
3+
"version": "1.0.0",
44
"description": "Appends script tags to the document",
55
"author": "Olavo Parno",
66
"license": "MIT",

0 commit comments

Comments
 (0)