Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import ErrorUtils in React Native

Tags:

react-native

  • RN version: 0.50
  • Testing on Android, haven't tested on iOS

I am trying to use ErrorUtils.setGlobalHandler as described in this github issue: https://github.com/facebook/react-native/issues/1194

However, what is not clear from the issue is how to import ErrorUtils. It's not in the react documentation: https://facebook.github.io/react-native/docs/0.50/getting-started.html

Previously, in RN 0.41, I was able to import ErrorUtils with import ErrorUtils from "ErrorUtils"; However, in 0.50 I am getting a red react popup with the following message when I try to import ErrorUtils like this:

com.facebook.react.common.JavascriptException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://localhost:8081/index.bundle?platform=android&dev=true&minify=false' failed to load.

I've also tried import { ErrorUtils } from 'react-native'; but it doesn't seem to exist there. The error is:

Cannot read property 'setGlobalHandler' of undefined

How do I properly import ErrorUtils in RN 0.50?

like image 848
Chris Avatar asked Oct 26 '18 20:10

Chris


3 Answers

ErrorUtils is a global variable, therfore it doesn't need to be imported. You can verify this with console.log(global.ErrorUtils)

However it is exported as module anyways (here). The comment there also has more information why it is done this way.

You can import the module like this:

import ErrorUtils from 'ErrorUtils';

like image 190
Leo Avatar answered Nov 19 '22 21:11

Leo


For anyone on RN61+, you should no longer import the module as you will experience the following error in the running metro bundler:

Error: Unable to resolve module `ErrorUtils`

Instead, just use the module without importing as this is a global variable, as stated by leo

like image 29
Brendan Bell Avatar answered Nov 19 '22 23:11

Brendan Bell


I did created a global.d.ts to define a global variable,

interface Global {

   ErrorUtils: {
      setGlobalHandler: any
      reportFatalError: any
      getGlobalHandler: any
   }

}

declare var global: Global

then, at where you are trying to use it, simply

global.ErrorUtils.setGlobalHandler(xxxx)
like image 1
iceteacup Avatar answered Nov 19 '22 22:11

iceteacup