88[ ![ GitHub PRs] ( https://img.shields.io/github/issues-pr/ghengeveld/react-webworker.svg )] ( https://github.com/ghengeveld/react-webworker/pulls )
99
1010React 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
1212any messages or errors it sends, as well as the ` postMessage ` handler.
1313
1414- Zero dependencies
@@ -35,7 +35,7 @@ Using render props for ultimate flexibility:
3535import WebWorker from " react-webworker"
3636
3737const MyComponent = () => (
38- < WebWorker path = " /worker.js" >
38+ < WebWorker url = " /worker.js" >
3939 {({ data, error, postMessage }) => {
4040 if (error) return ` Something went wrong: ${ error .message } `
4141 if (data)
@@ -57,7 +57,7 @@ Using helper components (don't have to be direct children) for ease of use:
5757import WebWorker from " react-webworker"
5858
5959const MyComponent = () => (
60- < WebWorker path = " /worker.js" >
60+ < WebWorker url = " /worker.js" >
6161 < WebWorker .Pending >
6262 {({ postMessage }) => < button onClick= {() => postMessage (" hello" )}> Hello< / button> }
6363 < / WebWorker .Pending >
@@ -78,10 +78,13 @@ const MyComponent = () => (
7878
7979` <WebWorker> ` takes the following properties:
8080
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
8283- ` onMessage ` {Function} Callback function invoked when a message is received, passing message data as argument
8384- ` onError ` {Function} Callback function invoked when an error is received, passing error object as argument
8485
86+ > ` url ` and ` options ` are evaluated at mount time, so they must be defined immediately and won't respond to changes.
87+
8588### Render props
8689
8790` <WebWorker> ` provides the following render props:
@@ -101,7 +104,7 @@ const MyComponent = () => (
101104import WebWorker from " react-webworker"
102105
103106const MyComponent = () => (
104- < WebWorker path = " /worker.js" >
107+ < WebWorker url = " /worker.js" >
105108 {({ data, error, postMessage, updatedAt, lastPostAt }) => (
106109 < div>
107110 {data && (
@@ -119,6 +122,18 @@ const MyComponent = () => (
119122)
120123```
121124
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+
122137## Helper components
123138
124139` <WebWorker> ` provides several helper components that make your JSX even more declarative.
0 commit comments