8
8
[ ![ GitHub PRs] ( https://img.shields.io/github/issues-pr/ghengeveld/react-webworker.svg )] ( https://github.com/ghengeveld/react-webworker/pulls )
9
9
10
10
React component for easy communication with a Web Worker. Leverages the Render Props pattern for ultimate flexibility as
11
- well as the new Context API for ease of use. Just specify the public path to your Web Worker and you'll get access to
11
+ well as the new Context API for ease of use. Just specify the public url to your Web Worker and you'll get access to
12
12
any messages or errors it sends, as well as the ` postMessage ` handler.
13
13
14
14
- Zero dependencies
@@ -35,7 +35,7 @@ Using render props for ultimate flexibility:
35
35
import WebWorker from " react-webworker"
36
36
37
37
const MyComponent = () => (
38
- < WebWorker path = " /worker.js" >
38
+ < WebWorker url = " /worker.js" >
39
39
{({ data, error, postMessage }) => {
40
40
if (error) return ` Something went wrong: ${ error .message } `
41
41
if (data)
@@ -57,7 +57,7 @@ Using helper components (don't have to be direct children) for ease of use:
57
57
import WebWorker from " react-webworker"
58
58
59
59
const MyComponent = () => (
60
- < WebWorker path = " /worker.js" >
60
+ < WebWorker url = " /worker.js" >
61
61
< WebWorker .Pending >
62
62
{({ postMessage }) => < button onClick= {() => postMessage (" hello" )}> Hello< / button> }
63
63
< / WebWorker .Pending >
@@ -78,10 +78,13 @@ const MyComponent = () => (
78
78
79
79
` <WebWorker> ` takes the following properties:
80
80
81
- - ` path ` {string} (required) Public path to the Web Worker file (from the root of your domain)
81
+ - ` url ` {string} (required) Public url to the Web Worker file (from the root of your domain)
82
+ - ` options ` {Object} Options passed to the Worker constructor
82
83
- ` onMessage ` {Function} Callback function invoked when a message is received, passing message data as argument
83
84
- ` onError ` {Function} Callback function invoked when an error is received, passing error object as argument
84
85
86
+ > ` url ` and ` options ` are evaluated at mount time, so they must be defined immediately and won't respond to changes.
87
+
85
88
### Render props
86
89
87
90
` <WebWorker> ` provides the following render props:
@@ -101,7 +104,7 @@ const MyComponent = () => (
101
104
import WebWorker from " react-webworker"
102
105
103
106
const MyComponent = () => (
104
- < WebWorker path = " /worker.js" >
107
+ < WebWorker url = " /worker.js" >
105
108
{({ data, error, postMessage, updatedAt, lastPostAt }) => (
106
109
< div>
107
110
{data && (
@@ -119,6 +122,18 @@ const MyComponent = () => (
119
122
)
120
123
```
121
124
125
+ ### Passing options to the Worker
126
+
127
+ ``` js
128
+ import WebWorker from " react-webworker"
129
+
130
+ const MyComponent = () => (
131
+ < WebWorker url= " /worker.js" options= {{ type: " module" , credentials: " include" }}>
132
+ ...
133
+ < / WebWorker>
134
+ )
135
+ ```
136
+
122
137
## Helper components
123
138
124
139
` <WebWorker> ` provides several helper components that make your JSX even more declarative.
0 commit comments