1

I'm trying to access the document in a react vr app. The app was created using the react-vr-cli. However, when I try to access the page I get the following error.

document is not defined
Lunny
  • 852
  • 1
  • 10
  • 23
  • This is strange. Can you post your poject somewhere? – Hristo Georgiev Jul 10 '17 at 12:04
  • sorry for the late reply. I found out whats wrong. the document was in a self invoking function and for some reason the scope of the function wasn't the window, its DedicatedWorkerGlobalScope. – Lunny Jul 10 '17 at 14:54
  • Hi, I checked again, the problem wasn't self invoking function. If I import a script, the script is ran in DedicatedWorkerGlobalScope instead of window. – Lunny Jul 10 '17 at 15:42

1 Answers1

2

The React VR instance is run inside a web worker, that's why you're getting DedicatedWorkerGlobalScope instead of window and why there's no global document.

This is just how React VR works and it's why there's a react-vr-web package that's in charge of communicating with the VR instance via postMessage.

Unfortunately web workers only have access to a subset of the features present on in the browser on the global scope. For some you can find workarounds in React VR like AsyncStorage instead of localStorage. For everything else you'll have to figure out a way of communicating outside the web worker via postMessage and with a listener on onmessage.

Another way of sending stuff from window to the VR instance is to implement a native module as a bridge between the two. You can find more info in Persistent bridge between a React VR component and native module code.

Valentin
  • 2,772
  • 1
  • 27
  • 40