dai-shi / react-hooks-worker

React custom hooks for web workers
MIT License
709 stars 17 forks source link

Worker sends error no matter what. #3

Closed ThinkSalat closed 5 years ago

ThinkSalat commented 5 years ago

I'm not sure what's going wrong. I coped the slow_fib.js from the examples. and am linking to it like this: let { result, error } = useWorker('./slow_fib.js', 29);

All I am getting is null for result and 'error' for error. I commented out the whole file, and got the same thing...

dai-shi commented 5 years ago

Hi,

./slow_fib.js should be accessible from the browser. So, you need to put the file in something like public, not src which is to be bundled by webpack or alike.

I know it's not very handy. I'm looking for better API.

ThinkSalat commented 5 years ago

How about something like this?

dai-shi commented 5 years ago

Interesting.

I was looking at these:

The problem for me is that I don't want to limit the API to only RPC style.

ThinkSalat commented 5 years ago

The use case I'm going for is to render a react chart in a webworker and send back the html string in a message using renderToString. Does this seem doable?

dai-shi commented 5 years ago

Although I never tried, I think it's technically doable if it's "String". (but then how would you apply it to DOM?)

In general, rendering react components in worker is not straightforward, and I know only react-native-dom successfully did that.

dai-shi commented 5 years ago

https://www.npmjs.com/package/react-hooks-worker/v/0.4.0

You no longer need to have a separate file. Just put a function inline. Please check out README and example/04.

Closing this issue. Feel free to open a new issue or continue discussion in this issue if it's relevant.